- 一个File对象isDirectory和isFile可以同时是false(在文件不存在的时候)
File file = new File("/export/不存在的文件.txt");
System.out.println("isDirectory:" + file.isDirectory());
System.out.println("isFile:" + file.isFile());
上面的代码执行输出
isDirectory:false
isFile:false
- Path的of和resolve方法分别用于String的路径拼接,Path和String的路径拼接;relativize可以实现相反的过程。
Path dirPath = Path.of("/export", "test");
System.out.println(dirPath);
Path filePath = dirPath.resolve("a.txt");
System.out.println(filePath);
System.out.println(dirPath.relativize(filePath));
如上代码输出
/export/test
/export/test/a.txt
a.txt
- Files读写文件字符串的编码默认是UTF8,并不是系统默认编码
public static Path writeString(Path path, CharSequence csq, OpenOption... options) throws IOException{
return writeString(path, csq, UTF_8.INSTANCE, options);
}
public static String readString(Path path) throws IOException {
return readString(path, UTF_8.INSTANCE);
}
|