| 方法 | 說明 |
void write(int c) | 寫入單字符 |
void write(char[] cbuf) | 寫入字符數組 |
void write(char[] cbuf, int off, int len) | 寫入數組片段 |
void write(String str) | 寫入字符串 |
void write(String str, int off, int len) | 寫入字符串片段 |
void flush() | 刷新緩衝區 |
void close() | 關閉流(先 flush) |
FileWriter 構造方法
// 覆蓋模式
new FileWriter(File file)
new FileWriter(String fileName)
// 追加模式
new FileWriter(File file, boolean append)
new FileWriter(String fileName, boolean append)
// 指定編碼(Java 11+)
new FileWriter(String fileName, StandardCharsets.UTF_8)
new FileWriter(String fileName, StandardCharsets.UTF_8, true)
實戰示例
public class WriterDemo {
public static void main(String[] args) throws IOException {
File file = new File("notes.txt");
// 1. 寫入字符串與字符數組
try (Writer fw = new FileWriter(file, StandardCharsets.UTF_8)) {
fw.write("第一行:Hello Writer\n");
fw.write("第二行:".toCharArray());
fw.write("部分字符串", 0, 3); // 寫 "部"
fw.write('\n');
fw.flush(); // 手動刷新(可選)
}
// 2. 追加模式
try (Writer fw = new FileWriter(file, StandardCharsets.UTF_8, true)) {
fw.write("追加行:Append mode\n");
}
}
}
Reader — 字符輸入流
核心方法
| 方法 | 說明 |
int read() | 讀取單字符,返回 0~65535,末尾 -1 |
int read(char[] cbuf) | 讀取到字符數組,返回實際字符數 |
int read(char[] cbuf, int off, int len) | 讀取到數組指定範圍 |
long skip(long n) | 跳過 n 字符 |
void close() | 關閉流 |
FileReader 構造方法(Java 11+ 支持指定編碼)
new FileReader(File file)
new FileReader(String fileName)
// Java 11+
new FileReader(String fileName, StandardCharsets.UTF_8)
高效讀取模板
public class ReaderDemo {
public static void main(String[] args) throws IOException {
File file = new File("notes.txt");
// 方式一:逐字符讀取(僅適合極小文件/教學)
try (Reader fr = new FileReader(file, StandardCharsets.UTF_8)) {
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
}
// 方式二:分塊讀取字符數組(推薦通用)
try (Reader fr = new FileReader(file, StandardCharsets.UTF_8)) {
char[] buffer = new char[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = fr.read(buffer)) != -1) {
sb.append(buffer, 0, len);
}
System.out.println(sb);
}
// 方式三:Java 11+ readNChars 簡化讀取
try (Reader fr = new FileReader(file, StandardCharsets.UTF_8)) {
char[] all = new char[(int) file.length()]; // 僅小文件
fr.readNChars(all, 0, all.length);
System.out.println(new String(all));
}
}
}
字節流與字符流轉換:InputStreamReader / OutputStreamWriter
當需要自定義編碼或處理網絡流、壓縮流等非文件源時,使用轉換流:
// 讀取 GBK 編碼文件
try (Reader reader = new InputStreamReader(
new FileInputStream("gbk.txt"), StandardCharsets.ISO_8859_1)) { // 或 "GBK"
// ...
}
// 寫入指定編碼
try (Writer writer = new OutputStreamWriter(
new FileOutputStream("out.txt"), StandardCharsets.UTF_8)) {
writer.write("指定 UTF-8 編碼");
}
最佳實踐總結
- 文本文件首選字符流:自動處理編碼,API 更直觀(直接操作 String/char[])。
- 顯式指定編碼:Java 11+ 構造方法支持
Charset 參數;舊版本用 InputStreamReader/OutputStreamWriter 包裝。
- 分塊讀寫:字符數組緩衝區 1024~4096,避免逐字符
read()/write(int) 性能極差。
- try-with-resources 必用:保證
close() 執行,釋放文件鎖。
- 大文本文件考慮 BufferedReader/BufferedWriter:按行讀取(
readLine())或大幅減少系統調用,下篇將詳細講解緩衝流。
- 上一篇文章: Java IO 基石:字節流 InputStream 與 OutputStream 深度解析
- 下一篇文章: Java IO 核心:字節流與字符流的區別、選型與最佳實踐
Copyright ©
程式碼之家 All Rights Reserved