Files的常用方法都有哪些?

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);
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章