java Writer与Reader详解

字符流

1. 字符输出流
  一个字符一个字符的读
  只能用来操作文本(不能写图片 音频 视频)
  Write(所有字符输出流的父类 抽象类)

  FileWrite(OutputStreamWriter的子类)
  构造方法(绑定写入的路径):
        文件
        字符串

  mac系统 一个中文字符3个字节 
  默认使用的UTF-8的编码表(通用的编码表)

  Windows系统下 一个中文字符 占2字节
  默认使用GBK的编码表(简体中文)
  FileWriter fw = new FileWriter("/Users/lanou/Desktop/Test/haha.txt");
        fw.write(100);
        // 注意字符输出流 在写入文件的时候
        // 需要调用刷新方法
        // 建议:每次写入 最好都刷新一次
        fw.flush();

        // 字符数组写入
        char[] c = {'l','o','n','g'}; 
        fw.write(c);
        fw.flush();

        fw.write(c, 1, 3);
        fw.flush();

        //使用字符串直接写入
        fw.write("字符串输入\n");
        fw.flush();
        fw.write("但使龙城飞将在\n不教胡马度阴山\n");
        fw.flush();

        fw.write("白日依山尽", 1, 2);

        // 关闭资源前 会刷新
        fw.close();

    }
2.字符输入流
  Reader(所有字符输入流的父类 抽象类)

  写的时候可以直接写入字符串
  读的时候不可以 字符串很难界定到哪结束 不太容易判断一个字符串

public class Demo05 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("/Users/lanou/Desktop/Test/haha.tx");
        //循环读取
        int len = 0;
        while ((len = fr.read()) != -1) {
            System.out.println((char)len);
        }
        //字符数组
        char[] c = new char[1024];
        int num = 0;
        while ((num = fr.read(c)) != -1) {
            System.out.println(new String(c, 0, num));
        }
        fr.close();

    }
}

利用字符流 复制文件(异常处理)

3.public static void main(String[] args) {
        File src = new File("/Users/lanou/Desktop/Test/haha.txt") ;
        File dest = new File("/Users/lanou/Desktop/Test/haha01.txt") ;
        copy(src, dest);
    }
    public static void copy(File src, File dest) {
        //增加作用域
        FileWriter fw = null;
        FileReader fr = null;
        try {
            //创建流
            fw = new FileWriter(dest);
            fr = new FileReader(src);
            //写入
            char[] c = new char[1024];
            int len = 0;
            while ((len = fr.read(c)) != -1) {
                fw.write(c, 0, len);
                fw.flush();
            }
        } catch (FileNotFoundException e) {
            //抛出一个运行时异常(直接停止掉程序)
            throw new RuntimeException("文件找不到");
        } catch (IOException e) {
            //文件复制失败 直接停止程序
            throw new RuntimeException("文件复制失败");
        } finally {
            try {
                //如果是空的 说明流创建失败 失败了不需要关闭
                if (fw == null) {
                    fw.close();
                }
            } catch (IOException e) {
                //关闭资源失败 停止程序
                throw new RuntimeException("关闭资源失败");
            }finally {
                try {
                    if (fr == null) {
                        fr.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("关闭资源失败");
                }
            }
        }
    }

转换流

4.转换流图解
java Writer与Reader详解_第1张图片

5. OutputStreamWriter(字符流转向字节流)
   作用: 可以根据不同编码格式写入
   需要使用 FileOutputStream类

   InputStreamReader(字节流转向字符流)
   作用: 可以读取不同编码格式的文件
   需要使用到 FileInputStream类
//利用转换流写文件 OutputStreamWriter 默认UTF8写
    public static void getUTF8() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/utf8.txt");
        //创建转换流 字符流转向字节流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        //写文件
        osw.write("丁鹏");
        //关闭资源(只关闭外层的流就可以了)
        osw.close();
    }

    //利用转换流使用GBK的编码写入文件
    public static void getGBK() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/gbk.txt");
        //构建转换流 传入编码格式(编码格式的字符串 忽略大小写)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        osw.write("丁鹏");
        osw.close();
    }

    //利用转换流 读取UTF-8编码的文件(以UTF-8的形式读取文件)
    public static void readUTF8() throws IOException{
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/utf8.txt");
        //创建转换流 字节流转向字符流
        InputStreamReader isr = new InputStreamReader(fis);
        //读文件
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }

    //利用转换流 读取GBK编码的文件(以GBK的形式读取文件)
    public static void readGBK() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/Test/gbk.txt");
        //构建转换流 传入编码格式(编码格式的字符串 忽略大小写)
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        //读文件
        char[] c = new char[1024];
        int len = 0;
        while ((len = isr.read(c)) != -1) {
            System.out.println(new String(c, 0, len));
        }
        isr.close();
    }

你可能感兴趣的:(java)