Home  >  Article  >  Java  >  File system operations using the new Files and Path classes in Java 11

File system operations using the new Files and Path classes in Java 11

WBOY
WBOYOriginal
2023-07-30 22:25:32984browse

Use the new Files class and Path class in Java 11 for file system operations

Java 11 introduces a number of new classes and features, including improvements to file system operations. The new Files class and Path class provide a more convenient and flexible file operation method, allowing us to process files and directories more easily.

Before Java 11, we usually used the java.io.File class to operate files and directories. However, the functionality of this class is relatively limited and not flexible enough. The new Files class and Path class introduced in Java 11 provide more functionality and operation options.

First, let's take a look at how to use the Path class to represent file paths. The Path class represents a path in the file system, which can be a path to a file or directory. We can use the Path.of() method to create a Path object, for example:

Path path = Path.of("C:\Users\Bob\Desktop\test.txt");

The above code creates a Path object using an absolute path. We can also use relative paths to create Path objects, for example:

Path path = Path.of("files\test.txt");

It should be noted that relative paths are relative to the path of the current working directory.

Next, we can use the Files class to perform some file operations. Here are some common file operation examples:

  1. Check if the file exists:
if (Files.exists(path)) {
    System.out.println("文件存在");
} else {
    System.out.println("文件不存在");
}
  1. Create the file:
try {
    Files.createFile(path);
    System.out.println("文件创建成功");
} catch (IOException e) {
    e.printStackTrace();
}
  1. Create directory:
try {
    Files.createDirectory(path);
    System.out.println("目录创建成功");
} catch (IOException e) {
    e.printStackTrace();
}
  1. Copy file:
Path targetPath = Path.of("C:\Users\Bob\Desktop\target.txt");
try {
    Files.copy(path, targetPath, StandardCopyOption.REPLACE_EXISTING);
    System.out.println("文件复制成功");
} catch (IOException e) {
    e.printStackTrace();
}
  1. Delete file or directory:
try {
    Files.delete(path);
    System.out.println("文件或目录删除成功");
} catch (IOException e) {
    e.printStackTrace();
}

above Just some basic file manipulation examples. The Files class also provides many other functions, such as moving files, renaming files, traversing directories, and more. We can choose the corresponding method to implement file system operations according to specific needs.

In addition to the above examples, Java 11 also provides some other useful features. For example, we can use the Files.lines() method to read all the lines of the file as follows:

try {
    List<String> lines = Files.lines(path).collect(Collectors.toList());
    for (String line : lines) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

In this example, we use the Files.lines() method to read all the lines of the file, and save it in Listf7e83be87db5cd2d9a8a0b8117b38cd4. We can then process each row.

In summary, the new Files class and Path class in Java 11 provide a more convenient and flexible file system operation method. We can use the Path class to represent file paths and the Files class for file and directory operations. These new classes and functions make file system operations simpler and more efficient.

I hope this article will help you understand and use the new Files class and Path class in Java 11. Wish you happy using it!

The above is the detailed content of File system operations using the new Files and Path classes in Java 11. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn