首页 » Java » Java IO流 字符流 与 转换流 详解

Java IO流 字符流 与 转换流 详解

原文 http://blog.csdn.net/ForeverNagisa/article/details/79221394

2018-02-01 02:00:38阅读(291)

字符

一个字符字符的读 只能用来操作文本(不能写其他格式)

字符输出流(Writer)

写入字符流的抽象类

实现子类 FileWriter
代码示例:

public class Demo04 {
    public static void main(String[] args) throws IOException {
        FileWriter fWriter = new FileWriter("/Users/james/Desktop/level/haha.txt");
        fWriter.write(100);
        // 注意 字符输出流 在写入文件的时候需要调用刷新方法
        // 建议 每次写入最好都刷新一次
        fWriter.flush();
        char[] c = {'l','o','n','g'};
        fWriter.write(c);
        fWriter.flush();
        fWriter.write(c, 1,3);
        fWriter.flush();
        // 使用字符串直接写入
        fWriter.write("抽刀断水水更流\n");
        fWriter.flush();
        fWriter.write("借酒消愁愁更愁\n");
        fWriter.flush();
        fWriter.write("百日依山尽", 1, 2);
        // 关闭资源前 刷新
        fWriter.close();
    }
}
字符输入流(Reader)

用于读取字符流的抽象类

实现子类:FileReader
代码示例:

public class Demo05 {
    public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader("/Users/james/Desktop/level/haha.txt");
        // 循环读取
        char[] bs = new char[1024];
        int len = 0;
        while ((len = reader.read(bs)) != -1) {
            System.out.println(new String(bs, 0, len));
        }
        reader.close();
    }
}

字符流中输出流中可以直接写字符串类型 但是输入流才行 这是为什么呢?

因为字符串类型很难判断什么时候结束 因此输入流不能直接读字符串

例题联系:

利用字符流复制文件

public class Demo06 {
    public static void main(String[] args) {
        File file = new File("/Users/james/Desktop/level/haha.txt");
        File file2 = new File("/Users/james/Desktop/level/ha.txt");
        copyFileTxt(file, file2);
    }
    // 字符流复制文件
    public static void copyFileTxt(File src ,File dest) {
        FileReader reader = null;
        FileWriter writer = null;
        try {
            reader = new FileReader(src);
            writer = new FileWriter(dest);
            char[] c = new char[1024];
            int len = 0;
            while ((len = reader.read(c)) != -1) {
                writer.write(c, 0, len);
            }               
        } catch (FileNotFoundException e) {
            throw new RuntimeException("找不到文件");
        } catch (IOException e) {
            throw new RuntimeException("编译异常");
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }               
            } catch (IOException e) {
                throw new RuntimeException("资源关闭异常");
            }finally {
                try {
                    if (writer != null) {
                        writer.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException("资源关闭异常");
                }
            }
        }
    }
}
转换流 OutputStreamWriter(字符流转向字节流)

字符流转向字节流 可以使用不同编码格式写入 需要使用到FileOutStream类

InputStreamReader(字节流转向字符流)

可以读取不同编码格式的文件 需要使用到 FileInputStream类

<a href=Java IO流 字符流 与 转换流 详解" src="http://img.blog.csdn.net/20180131194412978?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRm9yZXZlck5hZ2lzYQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描述" title="">

代码示例:

public class Demo07 {
    public static void main(String[] args) throws IOException {
        //getUTF8();
//      getGBK();
//      readUTF8();
        readGBK();
    }
    // 利用转换流写文件 OutputStreamWriter 默认UTF-8写
    public static void getUTF8() throws IOException {
        FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/utf-8.txt");
        // 创建转换流 字符流转向字节流
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        // 写文件
        osw.write("雪中智代雨中杏,樱下小渚花田汐");
        // 关闭资源(只关闭外层流 就可以)
        osw.close();
    }
    // 利用转换流写文件 OutputStreamWriter 使用GBK
    public static void getGBK() throws IOException {
            FileOutputStream fos = new FileOutputStream("/Users/james/Desktop/level/GBK.txt");
        // 构建转换流 传入编码格式(编码格式的字符串 忽略大小写)
        OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
        osw.write("雪中智代雨中杏,樱下小渚花田汐");
        osw.close();    
    }
    public static void readUTF8() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/utf-8.txt");
        InputStreamReader isr = new InputStreamReader(fis); 
        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        isr.close();
    }
    public static void readGBK() throws IOException {
        FileInputStream fis = new FileInputStream("/Users/james/Desktop/level/GBK.txt");
        InputStreamReader isr = new InputStreamReader(fis,"GBK"); 
        int len = 0;
        char[] b = new char[1024];
        while ((len = isr.read(b)) != -1) {
            System.out.println(new String(b, 0, len));
        }
        isr.close();
    }
}

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹