The reason for adding this class is that the java.io.File class has many shortcomings:
1. Many methods in the java.io.File class fail without exception handling or throwing an exception.
The java.io.File.delete() method returns a Boolean value indicating success or failure but without cause of failure.
2.Path is fast and convenient.
Original address: http://blog.csdn.net/LuoZheng4698729/article/details/51697648
Path operation
1. Delete files
2. Traversing directories, excluding subdirectories
Path dir = Paths.get("F:\\LZ\\pdf");
DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
for(Path path : stream){
System.out.println(path.getFileName());
}
- 1
- 2
- 3
- 4
- 5
2. Traversing directories and files in their subdirectories
Path dir = Paths.get("F:\\LZ\\pdf");
Files.walkFileTree(dir,new SimpleFileVisitor<Path>(){
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if(file.toString().endsWith(".pdf")){
System.out.println(file.getFileName());
}
return super.visitFile(file, attrs);
}
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
3. Create multilevel directories
Path dir = Paths.get("F:\\LZ\\xx\\dd");
Files.createDirectories(dir);
- 1
- 2
4. Create a file and throw an exception if it does not exist
Path dir = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
Files.createFile(dir);
- 1
- 2
5. File duplication
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
Path target = Paths.get("F:\\LZ\\xx\\dd\\2.txt");
//REPLACE_EXISTING: Files exist and are replaced
Files.copy(src, target,StandardCopyOption.REPLACE_EXISTING);
- 1
- 2
- 3
- 4
- 5
6. Read files line by line
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
BufferedReader reader = Files.newBufferedReader(src,StandardCharsets.UTF_8);
String line ;
while((line=reader.readLine()) != null){
System.out.println(line);
}
reader.close();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
7. Write strings
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
BufferedWriter writer = Files.newBufferedWriter(src, StandardCharsets.UTF_8
,StandardOpenOption.APPEND); // Append
writer.write("hello word Path");
writer.close();
- 1
- 2
- 3
- 4
- 5
8. Binary reads and writes are similar to strings
- A method to extract strings and binary streams directly
// Character string
Path src = Paths.get("F:\\LZ\\xx\\dd\\1.txt");
for(String line : Files.readAllLines(src)){
System.out.println(line);
}
// Binary stream
byte[] bytes = Files.readAllBytes(src);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
10. Monitor whether files in directories are modified, created, or deleted.
11. Read the last 1000 characters of the file
Official documents: http://docs.oracle.com/javase/7/docs/index.html
When I learned how to use Lucene 5.0, I found that I could not click on Path at any point. Later, I found something inside JDK7 on the internet. Then I went to get Path objects myself, but I didn't know how to get Path. So I went to find out how to get Path. I found this article, demonstrated it in person, and published it in the hope that it would be helpful.
Examples are as follows:
Operation results: