流:文本文件的讀寫

一、緩衝讀取文本:
關鍵點:
(1).
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream));
(2).循環判斷條件
String temp = br.readLine() != null;

package pdfSearchKeyWords;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class ReadAndWrite {
    public static void main(String[] args){
        String path = "C:\\Users\\Administrator\\Desktop\\jar\\lxl.txt";
        String tagPath = "C:\\Users\\Administrator\\Desktop\\lxl.txt";
        copyTxt(path,tagPath);
    }

    public static void copyTxt(String path,String tagPath){
        FileInputStream fis = null;
        InputStreamReader reader =null;
        BufferedReader br = null;
        StringBuffer sb = new StringBuffer();
        BufferedWriter bw = null;
        try {
            //fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\jar\\lxl.txt");
            fis = new FileInputStream(path);
            reader = new InputStreamReader(fis);
            br = new BufferedReader(reader);

            String temp=null;
            while((temp = br.readLine())!=null){
                sb.append(temp);
            }
            //bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\lxl.txt")));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(tagPath)));
            System.out.println(sb);
            bw.write(sb.toString());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            try {
                br.close();
                reader.close();
                fis.close();
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章