
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Primitive Data to Wrapper Class in Java
Java provides certain classes called wrapper classes in the java.lang package. The objects of these classes wrap primitive datatypes within them.
Using wrapper classes, you can also add primitive datatypes to various Collection objects such as ArrayList, HashMap etc. You can also pass primitive values over network using wrapper classes.
Example
import java.util.Scanner; public class WrapperExample { public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int i = sc.nextInt(); //Wrapper class of an integer Integer obj = new Integer(i); //Converting Integer object to String String str = obj.toString(); System.out.println(str); //Comparing with other object int result = obj.compareTo(new Integer("124")); if(result==0) { System.out.println("Both are equal"); }else{ System.out.println("Both are not equal"); } } }
Output
Enter an integer value: 1211 1211 Both are not equalHow to create and use directories in Java?
Creating directories
Just like the file class the Files class of java.nio package provides createTempFile() method which accepts two String parameters representing the prefix and suffix of and creates a temp file with specified details.
The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.
Example
Following example creates a new directory using the createDirectory() method of the Files class.
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main(String args[]) throws IOException { //Creating a path object String pathStr = "D:\sample_directory "; Path path = Paths.get(pathStr); //Creating a directory Files.createDirectory(path); System.out.println("Directory created successfully"); } }
Output
Directory created successfully
If you verify, you can observe see the created directory as −
Listing contents of a directory
The newDirectoryStream() method of the Files class opens the directory in the given path and returns the directory stream which gives the contents of the directory.
Example
import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FilesExample { public static void main(String args[]) throws IOException { //Creating a path object String pathStr = "D:\ExampleDirectory"; Path path = Paths.get(pathStr); System.out.println("Contents off the specified directory"); DirectoryStream stream = Files.newDirectoryStream(path); for (Path file: stream) { System.out.println(file.getFileName()); } } }
Output
Contents off the specified directory demo1.pdf demo2.pdf sample directory1 sample directory2 sample directory3 sample directory4 sample_jpeg1.jpg sample_jpeg2.jpg test test1.docx test2.docx
Using directory filters
You can filter the directory using the DirectoryStream.Filter following example filters the directories in the specified path.
Example
import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Test { public static void main(String args[]) throws IOException { //Creating a path object String pathStr = "D:\ExampleDirectory"; Path path = Paths.get(pathStr); System.out.println("Directories in the specified directory"); DirectoryStream.Filter filter = new DirectoryStream.Filter(){ public boolean accept(Path file) throws IOException { return (Files.isDirectory(file)); } }; DirectoryStream list = Files.newDirectoryStream(path, filter); for (Path file : list) { System.out.println(file.getFileName()); } } }
Output
Directories in the specified directory hidden directory1 hidden directory2 sample directory1