首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Java 文件讀寫核心 API 實戰:FileReader/Writer、BufferedReader/Writer、字節流對比

     編輯:Java 綜合教學

Java 文件讀寫核心 API 實戰:FileReader/Writer、BufferedReader/Writer、字節流對比

一、字符流讀取文本文件(推薦方式)

1. 基礎:FileReader + BufferedReader

File file = new File("D:/1.txt");
try (FileReader fr = new FileReader(file);
     BufferedReader br = new BufferedReader(fr)) {
    
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = br.readLine()) != null) {
        sb.append(line).append("\n");
    }
    System.out.println(sb.toString());
}

2. 進階:Files.readAllLines (JDK 7+)

List lines = Files.readAllLines(Paths.get("D:/1.txt"), StandardCharsets.UTF_8);
lines.forEach(System.out::println);

3. 大文件流式處理:Files.lines (JDK 8+)

try (Stream stream = Files.lines(Paths.get("D:/1.txt"), StandardCharsets.UTF_8)) {
    stream.filter(l -> l.contains("ERROR"))
          .limit(100)
          .forEach(System.out::println);
}

二、字符流寫入文本文件

1. 基礎:FileWriter + BufferedWriter

File out = new File("D:/2.txt");
if (!out.exists()) out.createNewFile();

try (FileWriter fw = new FileWriter(out);
     BufferedWriter bw = new BufferedWriter(fw)) {
    
    bw.write("第一行內容");
    bw.newLine(); // 跨平台換行
    bw.write("第二行內容");
    bw.flush(); // 確保寫入磁盤
}

2. 便捷:Files.write (JDK 7+)

List lines = Arrays.asList("行1", "行2", "行3");
Files.write(Paths.get("D:/3.txt"), lines, StandardCharsets.UTF_8, 
    StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);

三、字節流複製文件(二進制安全)

1. 傳統:FileInputStream + FileOutputStream + 緩衝數組

public static void copyByte(File src, File dest) throws IOException {
    if (!src.exists() || !src.isFile()) throw new IllegalArgumentException("無效源文件");
    
    try (FileInputStream in = new FileInputStream(src);
         FileOutputStream out = new FileOutputStream(dest)) {
        
        byte[] buffer = new byte[8 * 1024]; // 8KB 緩衝
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
    }
}

2. 高效:FileChannel.transferTo (JDK 4+,零拷貝)

public static void copyChannel(File src, File dest) throws IOException {
    try (FileChannel in = FileChannel.open(src.toPath(), StandardOpenOption.READ);
         FileChannel out = FileChannel.open(dest.toPath(), 
             StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
        
        long transferred = 0;
        long size = in.size();
        while (transferred < size) {
            transferred += in.transferTo(transferred, size - transferred, out);
        }
    }
}

3. 現代:Files.copy (JDK 7+,底層自動優化)

Files.copy(Paths.get("D:/1.txt"), Paths.get("D:/4.txt"), 
    StandardCopyOption.REPLACE_EXISTING);

四、性能實測對比(100MB 文件)

方式耗時特點
單字節 read/write~45 秒極慢,系統調用開銷巨大
字節數組 8KB 緩衝~120 毫秒經典推薦,兼容性好
BufferedInputStream 單字節~300 毫秒有緩衝但單字節調用
FileChannel.transferTo~80 毫秒零拷貝,內核態傳輸,最快
Files.copy~85 毫秒API 簡潔,內部自動選擇最優策略

五、編碼處理最佳實踐

  1. 顯式指定編碼:StandardCharsets.UTF_8,避免平台默認編碼差異
  2. 文本用字符流:Reader/Writer 內建編碼轉換
  3. 二進制用字節流:圖片、視頻、壓縮包、加密數據
  4. 跨平台換行:bw.newLine() 或 System.lineSeparator()

六、資源管理鐵律

  • JDK 7+ 必須用 try-with-resources:自動關閉、異常安全、抑制異常鏈
  • 舊代碼 finally 關閉需嵌套 try-catch,極易洩漏
  • 輸出流 flush() 後再 close(),確保緩衝區落盤

七、常見場景速查表

場景推薦 API關鍵點
讀取配置文件Files.readAllLines / Properties.load小文件、全量讀取
逐行處理大日誌Files.lines / BufferedReader流式、不佔內存
寫入報表/導出BufferedWriter / CSVPrinter批量、flush 控制
複製上傳文件Files.copy / FileChannel.transferTo大文件、零拷貝
讀取圖片/二進制Files.readAllBytes / FileInputStream字節流、Base64 編碼

總結

現代 Java 文件操作**首選 NIO.2 (Files/Paths/FileChannel)**:API 簡潔、性能優化、跨平台、編碼顯式。傳統 Stream API 僅用於維舊項目或特定場景。**核心原則:文本指定 UTF-8、二進制用字節、大文件流式處理、try-with-resources 管理資源**。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved