• 字符流Reader、Writer,字符转换流 OutputStreamWriter、InputStreamReader


    Reader、Writer是所有字符流的超类。

    Writer类方法

    void write(String str)

    void write(char[] cbuf, int off, int len);

    void flush()

    void close

    OutputStreamWriter构造方法字符编码默认为系统编码,可以不写

    FileWriter的append默认为false,可以不写。另一个构造(File file, boolean append)

            Writer out = new FileWriter("E:/a.txt", true);
            
            OutputStream ops = new FileOutputStream("E:/a.txt", true);
            Writer out = new OutputStreamWriter(ops, "GBK");  // 默认系统编码,可以不写

    Reader类

    int read(char[] cbuf)

    InputStreamReader与OutputStreamWriter类似

    FileReader 与 FileWriter类似’

            Reader in = new FileReader("E:/gbk.txt");
            
            InputStream inStream = new FileInputStream("E/gbk.txt");
            Reader in = new InputStreamReader(inStream, "gbk");

     BufferedReader,BufferedWriter使用与BufferedOutputStream, BufferedInputStream用法相同

    这段代码对比了4种方式进行字符流复制速度。

        public static void main(String[] args) throws IOException, InterruptedException {
    
            /*文件  114M 
             * 逐字符读,无缓冲    10252
             * 逐字符读,有缓冲    7730
             * 1024字符,无缓冲   3645
             * 1024字符   有缓冲    3578
             */
            long b = System.currentTimeMillis();
            copy_4();
            long a = System.currentTimeMillis();
            System.out.println(a-b);
        }
        
        public static void copy_1() throws IOException{
            Reader r = new FileReader("E:/test.pdf");
            BufferedReader in = new BufferedReader(r);
            
            Writer w = new FileWriter("E:/copy.pdf");
            BufferedWriter out = new BufferedWriter(w);
            
            int len;
            while((len = in.read()) != -1)
            {
                out.write(len);
            }
            
            in.close();
            out.close();
        }
        
        public static void copy_2() throws IOException{
            Reader in = new FileReader("E:/test.pdf");
            Writer out = new FileWriter("E:/copy.pdf");
            int len;
            while((len = in.read()) != -1)
            {
                out.write(len);
            }
            in.close();
            out.close();
        }
        public static void copy_3() throws IOException{
            Reader in = new FileReader("E:/test.pdf");
            Writer out = new FileWriter("E:/copy.pdf");
            int len;
            char[] cbuf = new char[1024];
            while((len = in.read(cbuf)) != -1)
            {
                out.write(cbuf,0,len);
                out.flush();
            }
            in.close();
            out.close();
        }
        
        public static void copy_4() throws IOException{
            Reader r = new FileReader("E:/test.pdf");
            BufferedReader in = new BufferedReader(r);
            
            Writer w = new FileWriter("E:/copy.pdf");
            BufferedWriter out = new BufferedWriter(w);
            
            int len;
            char[] cbuf = new char[1024];
            while((len = in.read(cbuf)) != -1)
            {
                out.write(cbuf,0,len);
                out.flush();
            }
            in.close();
            out.close();
        }
    View Code
  • 相关阅读:
    .NET 多线程 Task async await
    .NET5 MVC 文件目录
    Html 问题记录
    vue学习笔记(记录知识点)
    vue调试工具vue-devtools安装及使用
    node.js入坑记录
    vue从0开始笔记
    前端样式css问题记录
    谷歌浏览器chrome console 发送POST/GET请求
    jQuery的请求数据方式
  • 原文地址:https://www.cnblogs.com/YKang/p/7286642.html
Copyright © 2020-2023  润新知