zoukankan      html  css  js  c++  java
  • 字符流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
  • 相关阅读:
    Git 安装部署的详细说明
    jmeter数据库连接异常记录
    安装测试真的有那么简单吗?
    5G通信系统简单介绍
    postman 模拟服务器server
    再来对http协议做个详细认识
    关于Fiddler Everywhere的使用说明
    pom模式+ddt思想+logger+allure 重构jpress
    adb常见异常归类
    DDT思想
  • 原文地址:https://www.cnblogs.com/YKang/p/7286642.html
Copyright © 2011-2022 走看看