JAVA——緩衝流(字元流簡化流版本 FileReader和FileWriter )
阿新 • • 發佈:2020-09-02
package com.cskaoyan.io02.simplify;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;import java.io.Writer;
/**
* @author [email protected].
* @version 1.0
*
* FileReader和FileWriter是轉化流簡化版本
* 同時FileRader和FileWriter,專門操作檔案
* FileWriter
public FileWriter(String fileName),,
public FileWriter(String fileName, boolean append)
FileReader
public FileReader(String fileName)public FileReader(File file)
FileReader & FileWriter VS 轉化流:
1. 轉化流,建立物件麻煩,但是可以指定,編解碼使用的字符集
2. FileReader & FileWriter,建立物件簡單,但是,我們無法指定,其編解碼使用的字符集,即只能使用預設字符集來編解碼
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
// 太麻煩
//OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("e:\\a.txt"));//FileWriter fileWriter = new FileWriter("e:\\a.txt");
//
copyFileByChars("a.txt", "d:\\b.txt");
}
public static void copyFileByChars(String srcPath, String destPath) throws IOException {
// 1.建立字元輸入,輸出流物件
Writer writer = new FileWriter(destPath);
Reader reader = new FileReader(srcPath);
//2. 複製檔案(字元)
int len;
char[] charBuf = new char[1024];
while ((len = reader.read(charBuf)) != -1) {
writer.write(charBuf, 0, len);
}
//3. 關閉流
writer.close();
reader.close();
}
}