IO_字符流

字符流:只能处理 纯文本,全部为可见字符  ——简单理解为.txt 和 html文件

节点流:Reader  FileReader

              Writer   FileReader

一、纯文本读取

1、建立联系

2、选择流    Reader  FileReader

3、读取文件    char[ ] flu = new char[ 1024 ]

4、关闭

public class Readers {
    public static void main(String[] args) {
        //1、建立联系
        File src = new File("F:/test/1.txt");
        //2、选择流
        Reader reader=null;
        {
            try {
                reader = new FileReader(src);

                //3、读取

                char[] flu = new char[1024];//缓存数组
                int len = 0;
                while (-1!=(len=reader.read(flu))){
                    //字符数组转字符串
                    String str = new String(flu,0,len);
                    System.out.println(str);
                }
            } catch (FileNotFoundException e) {
                System.out.println("源文件不存在");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("文件读取失败");
                e.printStackTrace();
            }finally {
                //4、关闭
                if (null!=reader){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

二、纯文本写出

1、建立联系

2、选择流    Writer   FileReader

3、读取文件    write( 字符数组,0,长度)+flush( )

4、关闭

public class Writers {
    public static void main(String[] args) {
        //1、创建源
        File file = new File("F:/test/1.txt");
        //2、选择流
        Writer writer = null;
        try {
            writer = new FileWriter(file);

            //写出
            String str = "I love you!";
            writer.write(str);
            //强制写出
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (null!=writer) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

三、纯文本的拷贝

综合以上一、二

/**
 * 仅限字符的纯文本
 *
 * @author 
 * @date 2018/5/29 20:46
 */
public class CopyText {
    public static void main(String[] args) {
        //1、创建连接
        File src = new File("F:/test/1.txt");
        File des = new File("F:/test/p/100.txt");

        //选择流
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader(src);
            writer= new FileWriter(des);

            char[] flu = new char[1024];
            int len = 0;
            //持续读取、写入
            while (-1!=(len = reader.read(flu))){
                writer.write(flu,0,len);
            }
            writer.flush();//强制刷出

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                if(null!=writer){
                    writer.close();
                }
                if (null!=reader){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_30604989/article/details/80489770