Java NIO學習(一) Path、Paths 和 Files工具類的使用

JDK1.7 引入了新的IO操作類。在java.nio.file包下,包括Files、Paths等工具類。

中文文檔:http://www.matools.com/file/manual/jdk_api_1.8_google/java/nio/file/package-summary.html

1.創建文件或目錄

/**
 * 創建文件或目錄
 */
private static void createFileOrDir() {
    try {
        // 創建新目錄,除了最後一個部件,其他必須是存在的
        Files.createDirectory(Paths.get("F:/test"));

        // 創建路徑中的中間目錄,能創建不存在的中間部件
        Files.createDirectories(Paths.get("F:/test/test"));

        // 創建文件及相關目錄
        Path path = Paths.get("F:/test/testbak.txt");
        Files.createDirectories(path.getParent());
        Files.createFile(path);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

2.刪除文件

/**
 * 刪除文件
 */
private static void deleteFile() {
    Path p = Paths.get("F:/test.txt");
    try {
        Files.delete(p);// 用static boolean deleteIfExists(Path path)方法比較好
        System.out.println("刪除成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

3.移動文件

/**
 * 移動文件
 */
private static void moveFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("E:/test.txt");
    try {
        Files.move(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("移動成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

4.複製文件

/**
 * 複製文件
 */
private static void copyFile() {
    Path pSrc = Paths.get("F:/test.txt");
    Path pDest = Paths.get("F:/testbak.txt");
    try {
        Files.copy(pSrc, pDest, StandardCopyOption.REPLACE_EXISTING);
        System.out.println("複製成功");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

5.從文件讀取數據

/**
 * 從文件讀取數據
 */
private static void readFromFile() {
    Path p = Paths.get("F:/", "test.txt");
    try {
        byte[] bytes = Files.readAllBytes(p);
        System.out.println(new String(bytes));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

6.從文件按行讀取數據

/**
 * 從文件按行讀取數據
 */
private static void readByLineFromFile() {
    Path path = Paths.get("F:/", "test.txt");
    try {
    	// 方式1
        List<String> lines = Files.lines(path).collect(Collectors.toList());
        System.out.println(lines);
        
        // 方式2
        BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
        String str = null;
        while((str = reader.readLine()) != null){
            System.out.println(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

如果方式2指定的字符編碼不對,可能會拋出異常 MalformedInputException ,或者讀取到了亂碼:

7.向文件寫入數據

/**
 * 向文件寫入數據
 */
private static void write2File() {
    // 獲得路徑
    Path path = Paths.get("F:/", "test.txt");
    String info = "I love java really,你喜歡什麼?";
    try {
        // 方式1
        Files.write(path, info.getBytes("utf8"), StandardOpenOption.APPEND);

		// 方式2
        BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
        writer.write(info );
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

8.獲得文件路徑的幾種方法

/**
 * 獲得文件路徑的幾種方法
 */
private static void getFilePath() {
    File file = new File("F:/test.txt");
    // Path
    Path p1 = Paths.get("F:/", "test.txt");// F:\test.txt
    System.out.println(p1);

    Path p2 = file.toPath();
    System.out.println(p2);

    Path p3 = FileSystems.getDefault().getPath("F:/", "test.txt");
    System.out.println(p3);
}






參考地址:https://www.cnblogs.com/maxigang/p/9044192.html
參考地址:https://www.cnblogs.com/zxfei/p/10901364.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章