欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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);
    }
相关标签: Files