
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert primitive data into wrapper class using 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
- Related Articles
- How to convert Wrapper value array list into primitive array in Java?
- Primitive Wrapper Classes are Immutable in Java
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()
- Convert Long to numeric primitive data types in Java
- Convert Byte to numeric primitive data types in Java
- Convert Short to numeric primitive data types in Java
- Java Program to convert Java Float to Numeric Primitive Data Types
- Java Float Wrapper Class
- Java Program to convert Double to numeric primitive data types
- Java Program to wrap a Primitive DataType in a Wrapper Object
- How to convert JavaScript objects to primitive data types manually?
- Java primitive data types
- Which packages contain Wrapper class in Java?
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
