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
  • 相关阅读:
    T-SQL查询语句
    数据库和表的管理
    数据库概念
    IRF2实验
    IFR2笔记
    校园网双网出口实验案例
    双机热备实验
    华为H3C(NAT)实验
    BGP(边界网关协议)实验
    Hybrid实验
  • 原文地址:https://www.cnblogs.com/YKang/p/7286642.html
Copyright © 2011-2022 走看看