Updated March 28, 2023
Introduction to Java NIO File
Java nio is one of the package and it’s the useful API for to access the files which is mostly used to manipulate the files and other directories with utilizing the static methods that primarily operate on Path objects and as stated in the path interface with latest java releases its only in static methods that worked with files, directories and other files will makeup the class.
Key Takeaways
- Static methods for manipulating files and directories are included in the Java NIO Files class, and the majority of these methods use the Path object.
- A novel I/O model built on channels, buffers, and selectors is offered by Java NIO. As a result, the API’s core comprises these modules.
- A high-performance networking and file handling API and structure known as Java NIO (New Input/Output) serves as an alternative IO API for Java.
- Non-blocking IO Operation and Buffer oriented approach are the two java NIO operations.
- It’s a faster than regular Input and Output operations.
What is Java NIO File?
As stated in the path interface with the most recent Java releases, only static methods that worked with files, directories, and other files will comprise the class. Java nio is one of the packages, and its useful API for accessing the files is mostly used to manipulate the files and other directories with the use of static methods that primarily operate on Path objects. It has numerous methods for handling files in the file system and these methods will cover required files class if its not included we checked the javaDoc. There may still be a method for it in the File class.
How to Create Java NIO File Class?
A group of java programming language in APIs known as NIO that is New Input/Output provide features for intensive I/O operations. The java2standardedition(J2SE) to supplement the existing standard input and outputs. It is also created by using JSR latest editions as part of java community process. Mainly the nio has used with some reasons like Non-blocking IO operation that helps to read the data whichever we need to perform the file operation.
First we create a file with nio package and then to set the path with file class because of file class we need to create new nio package API its understand and recommended for creating files method. The path interface will help to resembles the java.io package which exactly same as file type extension and then to create path in new file. And also it does not generate the read file until we create it using the same files class. The path describes the location of the file with the help of method like createFile(Path variable) where as variable denotes the filePath these objects helps to convert the others.
Code:
package TestNG;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class NewTest {
public static void main(String[] args) {
Path pths = Paths.get("D:\\data\\Jan17.txt");
try {
Path filecreated = Files.createFile(pths);
System.out.println("The file was created at above location : "+filecreated);
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Above example is the basic step for creation of file by using nio package class and its methods.
- Here we first declare the Path interface and declared the variable with the help of its for to get the file path like drive and its folder.
- Once folder was created or existed in the user end then it automatically created the filename called jan17.txt file.
- Initially its in 0 size kb file with the help of Files.createFile(path reference) method along with its path interface reference to create the file successfully on the mentioned location.
How to Create Java NIO File API?
Using the createFile API and its Path object that related to represent the file which we want to create along with standard file. In the aforementioned test the path is empty when we initially examine it for following the createFile operation its discovered to be empty values. Classes for accessing files and file systems which are defined in the java.nio.file package. The java API is mainly defined as for to access file and file system characteristics. There are numerous methods for handling files in the system which provided by the Java NIO files class(java.nio.file.Files). The most popular of these methods will be covered in this Java NIO files guide manuals. So if we require once that will not included in the JavaDoc.
Code:
package TestNG;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class NewTest {
public static void main(String[] args) throws IOException {
Path pths = Paths.get("D:/January18.txt");
if (Files.exists(pths)) {
System.out.println("The mentioned file is already created");
} else {
Files.createFile(pths);
System.out.println("File was created successfully");
}
}
}
Methods of Java NIO File
There are numerous methods list which followed by the files class documentation that found. Out of all the methods in the java NIO files class we mainly tried to focus on some of the most significant ones.
Some of the methods in the Java NIO Files class is handling files in the file system.
1. createFile(Path filePath, FileAttribute attrs): This method will help to create a file by using supplied Path which provided by the Files class.
2. Copy(InputStream ins, Destination dst, CopyOptions): By using this type of functions all bytes from a given input stream and copied to a given destination file and the same method will return the total number of bytes which helps to read and write the long value. LinkOption will help to aforementioned values for the parameter.
3. Copy_attributes: Like the last-modified time attribute to the new file using these command.
4. REPLACE_EXISTING: If a file which already exists then these method should be used and also to replace it.
5. NOFOLLOW_LINKS: If a file which contains a symbolic link and it use only the link itself not helps to destination or target link which already copied.
6. CreateDirectories(Path pth, FileAttribute<?>…fattrb): The provided path is used to construct the missing parent directories with the help of createDirectories() method.
7. Delete(Path pths): It is used to remove a file from a given path. If the file does not exist at the supplied path or if it’s a directory and cannot be deleted or is not empty. Finally it throws NoSuchFileException exception.
8. Exists(Path pths): It is a method for determining whether a file is present at the given path. If the file is there then the method will return true otherwise its return false statement.
9. readAllBytes(Path pth): The method which reads every byte from the file at the specified path and returns a byte array which comprising file reads with bytes format.
10. size(Path pth): The size of the file is at the given path in bytes that can be obtained using the size(Path pths) method.
11. write(Path pth, byte[] bts, OpenOption opts): It is used to write the bytes of file at the provided path by using the arguments path including byte[] array bytes and OpenOption options as opts.
Example of Java NIO File
Given below is the example mentioned:
Code:
package TestNG;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
public class NewTest {
public static void main(String[] args) {
Path pth = Paths.get("D:/January18.txt");
String strs = "Welocme To My Domain";
Charset charss = Charset.forName("ISO-8859-1");
try {
Files.write(pth, strs.getBytes());
List<String> lst = Files.readAllLines(pth, charss);
for (String lne : lst) {
System.out.println(lne);
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
Output:
Conclusion
Path is an interface that was added to the Java NIO package with latest java versions. Mainly it represents the specific file system location and call the same interface on the java nio package. By using the package called java.nio.file.Path API to define and perform the data operations through help of the channels.
Recommended Articles
This is a guide to Java NIO File. Here we discuss the introduction, how to create java NIO file class and API, methods, examples. You can also look at the following articles to learn more –