java.nio.file.Path Methods
for working with files and directories in NIO
The java.nio package in Java, short for "New I/O," provides a powerful set of APIs for performing non-blocking I/O operations and working with files and directories. It was introduced in Java 1.4 to address the limitations of the older java.io package, which primarily supports blocking I/O operations. Using Path from java.nio.file:
Creating a
PathObject:Paths.get(String first, String... more): Constructs aPathobject from one or more strings representing elements of the path.Path path = Paths.get("example.txt");
Checking File Existence and Type:
Files.exists(Path path, LinkOption... options): Checks if the file or directory represented by thePathexists.Path path = Paths.get("example.txt"); if (Files.exists(path)) { System.out.println("File exists."); } else { System.out.println("File does not exist."); }Files.isRegularFile(Path path, LinkOption... options): Checks if thePathrepresents a regular file.Path path = Paths.get("example.txt"); if (Files.isRegularFile(path)) { System.out.println("It's a regular file."); }Files.isDirectory(Path path, LinkOption... options): Checks if thePathrepresents a directory.Path path = Paths.get("mydirectory"); if (Files.isDirectory(path)) { System.out.println("It's a directory."); }
Getting File Information:
toAbsolutePath(): Returns the absolute path of thePath.Path path = Paths.get("example.txt"); Path absolutePath = path.toAbsolutePath();getFileName(): Returns the file or directory name as aPath.Path path = Paths.get("example.txt"); Path fileName = path.getFileName();
Checking Permissions:
Files.isReadable(Path path): Checks if the file or directory is readable.Path path = Paths.get("example.txt"); if (Files.isReadable(path)) { System.out.println("File is readable."); }Files.isWritable(Path path): Checks if the file or directory is writable.Path path = Paths.get("example.txt"); if (Files.isWritable(path)) { System.out.println("File is writable."); }
The java.nio package is especially well-suited for scenarios where you need to perform high-performance I/O operations, work with large files, or handle many connections concurrently. It provides efficient mechanisms for handling non-blocking I/O, which is essential in many network communication and file processing applications.
Certainly! The java.nio package, introduced in Java 7, provides a more modern and efficient way to work with files and I/O operations. Here's an example demonstrating some common file operations using the java.nio.file package:
import java.io.IOException;
import java.nio.file.*;
public class NIOFileExample {
public static void main(String[] args) {
// Define a path to a directory
Path directoryPath = Paths.get("my_directory");
try {
// Create a directory if it doesn't exist
if (!Files.exists(directoryPath)) {
Files.createDirectory(directoryPath);
System.out.println("Directory created: " + directoryPath.toString());
}
// Create a file inside the directory
Path filePath = directoryPath.resolve("example.txt");
String content = "Hello, Java NIO!";
Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
System.out.println("File created: " + filePath.toString());
// Read the contents of the file
byte[] fileBytes = Files.readAllBytes(filePath);
String readContent = new String(fileBytes);
System.out.println("File content: " + readContent);
// Rename the file
Path renamedFilePath = directoryPath.resolve("renamed_example.txt");
Files.move(filePath, renamedFilePath);
System.out.println("File renamed to: " + renamedFilePath.toString());
// Delete the file
Files.delete(renamedFilePath);
System.out.println("File deleted: " + renamedFilePath.toString());
// Delete the directory
Files.delete(directoryPath);
System.out.println("Directory deleted: " + directoryPath.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}In this example:
We create a directory called "my_directory" using
Files.createDirectoryif it doesn't already exist.We create a file called "example.txt" inside the directory using
Files.write.We read the contents of "example.txt" using
Files.readAllBytesand display the content.We rename the file to "renamed_example.txt" using
Files.move.We delete "renamed_example.txt" using
Files.delete.Finally, we delete the "my_directory" directory using
Files.delete.
The java.nio.file package provides more advanced and efficient file handling capabilities compared to the older java.io package. It's particularly useful when dealing with large files, asynchronous I/O, and more complex file operations.
Last updated
Was this helpful?