- Files.exists():检测文件路径是否存在。
Path path = Paths.get("E:\\ceshi.txt");
System.out.println(Files.exists(path));
try {
System.out.println(Files.createFile(Paths.get("E:\\b.txt")));
} catch (IOException e) {
e.printStackTrace();
}
- Files.createDirectory():创建文件夹。
System.out.println(Files.createDirectory(Paths.get("E:\\abc")));
- Files.delete():删除一个文件或目录。
String str1="E:\\cccc.txt";
try {
Files.delete(Paths.get(str1));
} catch (IOException e) {
e.printStackTrace();
}
String str1="E:\\aaa.txt";
try {
Files.copy(Paths.get(str1),Paths.get("D:\\bbb.txt"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
FileSystems.getDefault().getPath()
String str1="E:\\aaa.txt";
Path path = Paths.get(str1);
Path path1 = FileSystems.getDefault().getPath("D:\\bbbb.txt");
try {
Files.move(path,path1,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println(Files.size(Paths.get("E:\\aaa.txt")));
} catch (IOException e) {
e.printStackTrace();
}
String str1="E:\\cccc.txt";
try {
System.out.println(Files.readAllLines(Paths.get(str1)));
} catch (IOException e) {
e.printStackTrace();
}
String str="大熊猫";
try {
Files.write(Paths.get("E:\\cccc.txt"),str.getBytes(), LinkOption.NOFOLLOW_LINKS);
} catch (IOException e) {
e.printStackTrace();
}