字符输出流写文本FileWriter类 字符输入流读取文本FileReader类 flush方法和close方法区别

15字符输出流写文本FileWriter类

* A: 字符输出流写文本FileWriter类
    * a: 方法介绍
        *  void write(int c)
            *  写入单个字符
        * void write(String str)  
            * 写入字符串
        * void write(String str, int off, int len) 
            * 写入字符串的某一部分
        * void write(char[] cbuf)  
            * 写入字符数组
        * abstract  void write(char[] cbuf, int off, int len)  
            *  写入字符数组的某一部分
    * b: 案例代码
        /*
         *   字符输出流
         *     java.io.Writer 所有字符输出流的超类
         *   写文件,写文本文件
         *   
         *   写的方法 write
         *     write(int c) 写1个字符
         *     write(char[] c)写字符数组
         *     write(char[] c,int,int)字符数组一部分,开始索引,写几个
         *     write(String s) 写入字符串
         *     
         *   Writer类的子类对象 FileWriter
         *   
         *   构造方法:  写入的数据目的
         *     File 类型对象
         *     String 文件名
         *     
         *   字符输出流写数据的时候,必须要运行一个功能,刷新功能
         *   flush()
         */
        public class WriterDemo {
            public static void main(String[] args) throws IOException{
                FileWriter fw = new FileWriter("c:\\1.txt");
                
                //写1个字符
                fw.write(100);
                fw.flush();
                
                //写1个字符数组
                char[] c = {'a','b','c','d','e'};
                fw.write(c);
                fw.flush();
                
                //写字符数组一部分
                fw.write(c, 2, 2);
                fw.flush();
                
                //写如字符串
                fw.write("hello");
                fw.flush();
                
                fw.close();
            }
        }

16字符输入流读取文本FileReader类

* A: 字符输入流读取文本FileReader类
    * a: 方法介绍
        *  int read() 
            * 读取单个字符
        * int read(char[] cbuf) 
            * 将字符读入数组
        * abstract  int read(char[] cbuf, int off, int len)  
            * 将字符读入数组的某一部分。
    * b: 案例代码
        /*
         *  字符输入流读取文本文件,所有字符输入流的超类
         *    java.io.Reader
         *  专门读取文本文件
         *  
         *  读取的方法 : read()
         *   int read() 读取1个字符
         *   int read(char[] c) 读取字符数组
         *   
         *   Reader类是抽象类,找到子类对象 FileReader
         *   
         *   构造方法: 绑定数据源
         *     参数:
         *        File  类型对象
         *        String文件名
         */
        public class ReaderDemo {
            public static void main(String[] args) throws IOException{
                FileReader fr = new FileReader("c:\\1.txt");
                /*int len = 0 ;
                while((len = fr.read())!=-1){
                    System.out.print((char)len);
                }*/
                char[] ch = new char[1024];
                int len = 0 ;
                while((len = fr.read(ch))!=-1){
                    System.out.print(new String(ch,0,len));
                }
                
                fr.close();
            }
        }

17flush方法和close方法区别

* A: flush方法和close方法区别
    *a: flush()方法
        * 用来刷新缓冲区的,刷新后可以再次写出,只有字符流才需要刷新
    *b: close()方法
        * 用来关闭流释放资源的的,如果是带缓冲区的流对象的close()方法,不但会关闭流,还会再关闭流之前刷新缓冲区,关闭后不能再写出 

18字符流复制文本文件

* A: 字符流复制文本文件
    * a: 案例代码
        /*
         *  字符流复制文本文件,必须文本文件
         *  字符流查询本机默认的编码表,简体中文GBK
         *  FileReader读取数据源
         *  FileWriter写入到数据目的
         */
        public class Copy_2 {
            public static void main(String[] args) {
                FileReader fr = null;
                FileWriter fw = null;
                try{
                    fr = new FileReader("c:\\1.txt");
                    fw = new FileWriter("d:\\1.txt");
                    char[] cbuf = new char[1024];
                    int len = 0 ;
                    while(( len = fr.read(cbuf))!=-1){
                        fw.write(cbuf, 0, len);
                        fw.flush();
                    }
                    
                }catch(IOException ex){
                    System.out.println(ex);
                    throw new RuntimeException("复制失败");
                }finally{
                    try{
                        if(fw!=null)
                            fw.close();
                    }catch(IOException ex){
                        throw new RuntimeException("释放资源失败");
                    }finally{
                        try{
                            if(fr!=null)
                                fr.close();
                        }catch(IOException ex){
                            throw new RuntimeException("释放资源失败");
                        }
                    }
                }
            }
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容