OutputStreamWriter,InputStreamReader,PrintWriter,BufferedReader

package day20150904;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
//(1)OutputStreamWriter --------------------
/**
 * 字符流只用於讀寫文本數據
 * 字符流是高級流,底層本質上還是讀寫字節
 * 
 * Reader,Writer:字符輸入輸出流的父類
 */
public class OutputStreamWriterDemo1 {

    public static void main(String[] args) {


        try {
            FileOutputStream fos = new FileOutputStream("t.txt");
//          fos.write("中國成立了".getBytes("utf-8"));
//          fos.close();

            //可指定字符集,沒指定則爲系統默認字符集(windows默認爲gbk,linux默認爲utf-8)
            //OutputStreamWriter osw = new OutputStreamWriter(fos);
            OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
            //這樣就可以直接寫字符串了
            osw.write("hello");
            osw.write("中華人民共和國");
            osw.write("成立了");
            osw.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}
package day20150904;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
//(2)InputStreamReader -------
public class InputStreamReaderDemo2 {

    public static void main(String[] args) {

        try {
            FileInputStream fis = new FileInputStream("t.txt");
            InputStreamReader isr = new InputStreamReader(fis,"utf-8");

            int d = -1;
            while((d=isr.read())!=-1){
                char c = (char)d;
                System.out.print(c);
            }

            isr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



    }

}
package day20150904;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
//(3)用字符流複製文件-----------------------
public class FileCopy3 {

    public static void main(String[] args) {
        try {
            // 把./src/day0831/FileCopy3.java 這個文件複製到項目根目錄下
            FileInputStream fis = new FileInputStream("."
                    +File.separator+"src"
                    +File.separator+"day0831"
                    +File.separator+"FileCopy3.java");

            FileOutputStream fos = new FileOutputStream("copy.java");

            InputStreamReader isr = new InputStreamReader(fis,"utf-8");
            OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");

            int d = -1;

            while((d=isr.read())!=-1){
                osw.write(d);
            }

            isr.close();
            osw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }


    }

}
package day20150904;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
//(4)PrintWriter ------------------
/**
 * PrintWriter:
 * 緩衝字符輸出流,帶有自動行刷新
 * 可以以行爲單位寫出字符串
 * 
 * BufferedWriter沒有PrintWriter用起來爽
 */
public class PrintWriterDemo4 {

    public static void main(String[] args) {
        //以下可用,但系統編碼集無法更改
        //PrintWriter pw = new PrintWriter("pw.txt");

        //可改變系統編碼集
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("pw.txt"),"utf-8");

            /**
             * true:自動行刷新:
             * 每當使用println方法(不是print)後,都會自動flush
             * 
             *注意:會增加寫的次數,降低寫出效率
             */
            PrintWriter pw = new PrintWriter(osw,true);
            pw.println("你好:");//寫完換行
            pw.print("hehe");
            pw.println("今天");
            pw.println("天氣不錯");
            osw.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }


    }

}
package day20150904;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
//(5)BufferedReader -------------
/**
 * BufferedReader:緩衝字符輸入流
 * 可以以行爲單位讀取字符串
 */
public class BufferedReader5 {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("pw.txt");
            InputStreamReader isr = new InputStreamReader(fis,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            /**
             * String readLine()
             * 讀取一行,讀取到換行符爲止
             * 如果返回值爲null,說明沒數據可讀了
             */
            String str = null;
            while((str=br.readLine())!=null){
                System.out.println(str);
            }

            br.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
package day20150904;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
//(6)讀寫文件  -----------------
public class FileReadWriteDemo6 {
    /**
     * 文件中內容如下
     * hello
     * 你好
     * 今天天氣不錯
     * 要求操作後文件內容如下
     * hello
     * 你好
     * 今天天氣不錯
     * hello你好今天天氣不錯
     */
    public static void main(String[] args){
        try {
            //讀進來
            FileInputStream fis = new FileInputStream("pw.txt");
            InputStreamReader isr = new InputStreamReader(fis,"utf-8");
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();

            String str = null;
            while((str=br.readLine())!=null){
                sb.append(str);
            }
            br.close();

            FileOutputStream fos = new FileOutputStream("pw.txt",true);
            //寫出方法1:
//          fos.write(sb.toString().getBytes("utf-8"));
//          fos.close();

            //寫出方法2:
            OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
            PrintWriter pw = new PrintWriter(osw);
            pw.println(sb);
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
發佈了0 篇原創文章 · 獲贊 7 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章