java.nio.file
包是Java 7引入的NIO.2(New I/O 2)的一部分,提供了一组强大且灵活的API,用于处理文件系统操作。与之前的java.io
包相比,java.nio.file
包提供了更丰富的功能和更好的性能,特别是在处理大文件和高并发文件操作时。
主要类和接口
1. Paths
Paths
类包含用于创建Path
实例的静态方法。
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathsExample {
public static void main(String[] args) {
Path path = Paths.get("/home/user/file.txt");
System.out.println("Path: " + path);
}
}
2. Path
Path
接口表示文件系统中的路径。它提供了一系列方法用于路径的操作,如获取路径信息、路径比较和路径解析。
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathExample {
public static void main(String[] args) {
Path path = Paths.get("/home/user/file.txt");
System.out.println("File name: " + path.getFileName());
System.out.println("Parent: " + path.getParent());
System.out.println("Root: " + path.getRoot());
System.out.println("Is absolute: " + path.isAbsolute());
}
}
3. Files
Files
类包含用于文件和目录操作的静态方法,如创建、删除、复制、移动文件和目录,以及读取和写入文件内容。
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class FilesExample {
public static void main(String[] args) {
Path path = Paths.get("/home/user/file.txt");
try {
if (!Files.exists(path)) {
Files.createFile(path);
System.out.println("File created: " + path);
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. FileSystems
FileSystems
类提供对文件系统的访问。
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class FileSystemsExample {
public static void main(String[] args) {
FileSystem fs = FileSystems.getDefault();
Path path = fs.getPath("/home/user/file.txt");
System.out.println("Path: " + path);
}
}
其他重要类和接口
- DirectoryStream:用于迭代目录中的条目。
- FileVisitor和SimpleFileVisitor:用于遍历文件树。
- LinkOption:用于配置如何处理符号链接。
- OpenOption:用于配置如何打开文件。
示例代码
读取文件内容
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.List;
public class ReadFileExample {
public static void main(String[] args) {
Path path = Paths.get("/home/user/file.txt");
try {
List<String> lines = Files.readAllLines(path);
lines.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
写入文件内容
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.Arrays;
public class WriteFileExample {
public static void main(String[] args) {
Path path = Paths.get("/home/user/file.txt");
try {
Files.write(path, Arrays.asList("Hello, World!", "This is a test."));
} catch (IOException e) {
e.printStackTrace();
}
}
}
复制文件
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public class CopyFileExample {
public static void main(String[] args) {
Path source = Paths.get("/home/user/source.txt");
Path target = Paths.get("/home/user/target.txt");
try {
Files.copy(source, target);
System.out.println("File copied.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
遍历目录
import java.nio.file.*;
import java.io.IOException;
public class DirectoryStreamExample {
public static void main(String[] args) {
Path dir = Paths.get("/home/user");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry : stream) {
System.out.println(entry.getFileName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
总结
java.nio.file
包提供了功能强大、灵活且高效的文件系统操作API,适用于各种文件和目录操作需求。通过这些API,开发者可以轻松地进行文件创建、删除、复制、移动、读取、写入和监控等操作。