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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章