/*
?* 簡單的讀/寫文本文件的示例
?* 這里包含了三個例子,即
?* 1. 將文件讀入到內(nèi)存(這里是StringBuffer)的例子
?* 2. 將內(nèi)容中的文本寫到文件
?* 3. 將一個文件的內(nèi)容讀出來寫入另一個文件中
?*??? 同時也展示了如果從輸入流中讀出來內(nèi)容寫入輸出流中(僅限文本流)
?* 三個例子可以獨立存在,所以根據(jù)需要只看其中一個就行了。
?*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
public final class AccessTextFile {
??? /**
???? * 1. 演示將流中的文本讀入一個 StringBuffer 中
???? * @throws IOException
???? */
??? public void readToBuffer(StringBuffer buffer, InputStream is)
??????? throws IOException {
??????? String line;??????? // 用來保存每行讀取的內(nèi)容
??????? BufferedReader reader = new BufferedReader(new InputStreamReader(is));
??????? line = reader.readLine();?????? // 讀取第一行
??????? while (line != null) {????????? // 如果 line 為空說明讀完了
??????????? buffer.append(line);??????? // 將讀到的內(nèi)容添加到 buffer 中
??????????? buffer.append("\n");??????? // 添加換行符
??????????? line = reader.readLine();?? // 讀取下一行
??????? }
??? }
??? /**
???? * 2. 演示將 StringBuffer 中的內(nèi)容讀出到流中
???? */
??? public void writeFromBuffer(StringBuffer buffer, OutputStream os) {
??????? // 用 PrintStream 可以方便的把內(nèi)容輸出到輸出流中
??????? // 其對象的用法和 System.out 一樣
??????? // (System.out 本身就是 PrintStream 對象)
??????? PrintStream ps = new PrintStream(os);??
??????? ps.print(buffer.toString());
??? }
??? /**
???? * 3*. 從輸入流中拷貝內(nèi)容到輸入流中
???? * @throws IOException
???? */
??? public void copyStream(InputStream is, OutputStream os) throws IOException {
??????? // 這個讀過過程可以參閱 readToBuffer 中的注釋
??????? String line;
??????? BufferedReader reader = new BufferedReader(new InputStreamReader(is));
??????? PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
??????? line = reader.readLine();
??????? while (line != null) {
??????????? writer.println(line);
??????????? line = reader.readLine();
??????? }
??????? writer.flush();???? // 最后確定要把輸出流中的東西都寫出去了
??????????????????????????? // 這里不關(guān)閉 writer 是因為 os 是從外面?zhèn)鬟M(jìn)來的
??????????????????????????? // 既然不是從這里打開的,也就不從這里關(guān)閉
??????????????????????????? // 如果關(guān)閉的 writer,封裝在里面的 os 也就被關(guān)了
??? }
??? /**
???? * 3. 調(diào)用 copyStream(InputStream, OutputStream) 方法拷貝文本文件
???? */
??? public void copyTextFile(String inFilename, String outFilename)
??????? throws IOException {
??????? // 先根據(jù)輸入/輸出文件生成相應(yīng)的輸入/輸出流
??????? InputStream is = new FileInputStream(inFilename);
??????? OutputStream os = new FileOutputStream(outFilename);
??????? copyStream(is, os);???? // 用 copyStream 拷貝內(nèi)容
??????? is.close(); // is 是在這里打開的,所以需要關(guān)閉
??????? os.close(); // os 是在這里打開的,所以需要關(guān)閉
??? }
??? public static void main(String[] args) throws IOException {
??????? int sw = 1;???? // 三種測試的選擇開關(guān)
??????? AccessTextFile test = new AccessTextFile();
???????
??????? switch (sw) {
??????? case 1: // 測試讀
??????? {
??????????? InputStream is = new FileInputStream("E:\\test.txt");
??????????? StringBuffer buffer = new StringBuffer();
??????????? test.readToBuffer(buffer, is);
??????????? System.out.println(buffer);???? // 將讀到 buffer 中的內(nèi)容寫出來
??????????? is.close();
??????????? break;
??????? }
??????? case 2: // 測試寫
??????? {
??????????? StringBuffer buffer = new StringBuffer("Only a test\n");
??????????? test.writeFromBuffer(buffer, System.out);
??????????? break;
??????? }
??????? case 3: // 測試拷貝
??????? {
??????????? test.copyTextFile("E:\\test.txt", "E:\\r.txt");
??????? }
??????????? break;
??????? }
??? }
}