Files的常用方法都有哪些?
程序员文章站
2022-06-01 22:35:12
...
Files.exists()
判断文件是否存在
public static void main(String[] args) throws Exception {
Path path = Paths.get("F:/ceshi13.txt");
boolean pathExists = Files.exists(path,new LinkOption[]{ LinkOption.NOFOLLOW_LINKS});
System.out.println(pathExists);
}
Files.createDirectory()
创建文件夹
public static void main(String[] args) {
Path path = Paths.get("F:/nnn");
try {
Files.createDirectory(path) ;
} catch (IOException e) {
e.printStackTrace();
}
}
Files.copy()
复制文件
public static void main(String[] args) throws IOException {
Path path = Paths.get("F:/ceshi.txt");
Files.copy(path,new FileOutputStream(new File("F:/nnn.txt"))) ;
}
Files.move()
移动文件
Path path = Paths.get("F:/nnn.txt");
Path path2 = Paths.get("F:/nnn/nnn.txt");
Files.move(path,path2) ;
Files.delete()
删除一个文件或目录(空目录):如果不存在会报错
Path path = Paths.get("F:/ccc");
Files.delete(path);
Files.deleteIfExists()
删除一个文件或目录(空目录):如果存在就删除
public static void main(String[] args) throws IOException {
Path path = Paths.get("F:/nnn");
Files.deleteIfExists(path);
}
上一篇: javaWeb之jsp(二)注释
下一篇: JSP入门