zoukankan      html  css  js  c++  java
  • java转换流和缓冲流

     转换流

    OutputStreamWriter类

    OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节。它的作用的就是,将字符串按照指定的编码表转成字节,在使用字节流将这些字节写出去。

    代码演示:

    public static void writeCN() throws Exception {
            //创建与文件关联的字节输出流对象
            FileOutputStream fos = new FileOutputStream("c:\cn8.txt");
            //创建可以把字符转成字节的转换流对象,并指定编码
            OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
            //调用转换流,把文字写出去,其实是写到转换流的缓冲区中
            osw.write("你好");//写入缓冲区。
            osw.close();
        }

    InputStreamReader类

    InputStreamReader 是字节流通向字符流的桥梁:它使用指定的字符编码表读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。

    代码演示

    public class InputStreamReaderDemo {
        public static void main(String[] args) throws IOException {
            //演示字节转字符流的转换流
            readCN();
        }
        public static void readCN() throws IOException{
            //创建读取文件的字节流对象
            InputStream in = new FileInputStream("c:\cn8.txt");
            //创建转换流对象 
            //InputStreamReader isr = new InputStreamReader(in);这样创建对象,会用本地默认码表读取,将会发生错误解码的错误
            InputStreamReader isr = new InputStreamReader(in,"utf-8");
            //使用转换流去读字节流中的字节
            int ch = 0;
            while((ch = isr.read())!=-1){
                System.out.println((char)ch);
            }
            //关闭流
            isr.close();
        }
    }

      转换流和子类区别

    1、操作的是文件。2、使用默认编码。

    总结:

    字节--->字符 : 看不懂的--->看的懂的。  需要读。输入流。 InputStreamReader

    字符--->字节 : 看的懂的--->看不懂的。  需要写。输出流。 OutputStreamWriter

    缓冲流

      字节缓冲流

     写入数据到流中,字节缓冲输出流 BufferedOutputStream

     读取流中的数据,字节缓冲输入流 BufferedInputStream

    public class BufferedOutputStreamDemo01 {
        public static void main(String[] args) throws IOException {
            
            //写数据到文件的方法
            write();
        }
    
        /*
         * 写数据到文件的方法
         * 1,创建流
         * 2,写数据
         * 3,关闭流
         */
        private static void write() throws IOException {
            //创建基本的字节输出流
            FileOutputStream fileOut = new FileOutputStream("abc.txt");
            //使用高效的流,把基本的流进行封装,实现速度的提升
            BufferedOutputStream out = new BufferedOutputStream(fileOut);
            //2,写数据
            out.write("hello".getBytes());
            //3,关闭流
            out.close();
        }
    }
    /*
         * 从文件中读取数据
         * 1,创建缓冲流对象
         * 2,读数据,打印
         * 3,关闭
         */
        private static void read() throws IOException {
            //1,创建缓冲流对象
            FileInputStream fileIn = new FileInputStream("abc.txt");
            //把基本的流包装成高效的流
            BufferedInputStream in = new BufferedInputStream(fileIn);
            //2,读数据
            int ch = -1;
            while ( (ch = in.read()) != -1 ) {
                //打印
                System.out.print((char)ch);
            }
            //3,关闭
            in.close();
        }

     使用基本的流与高效的流完成复制文件

    /*
     * 需求:将d:\test.avi文件进行复制
     *         采用4种方式复制
     *         方式1: 采用基本的流,一次一个字节的方式复制    共耗时 224613毫秒
     *         方式2: 采用基本的流,一个多个字节的方式赋值    共耗时     327毫秒
     *         方式3: 采用高效的流,一次一个字节的方式复制    共耗时    2047毫秒
     *         方式4: 采用高效的流,一个多个字节的方式赋值    共耗时      96毫秒
     * 
     * 数据源: d:\test.avi
     * 目的地1: d:\copy1.avi
     * 目的地2: d:\copy2.avi
     * 目的地3: d:\copy3.avi
     * 目的地4: d:\copy4.avi
     * 
     * 实现的步骤:
     *     1,指定数据源
     *     2,指定目的地
     *     3,读数据
     *     4,写数据
     *     5,关闭流
     * 
     */
    public class CopyAVI {
        public static void main(String[] args) throws IOException {
            //开始计时
            long start = System.currentTimeMillis();
            //方式1: 采用基本的流,一次一个字节的方式复制
            //method1("d:\test.avi", "d:\copy1.avi");
            //方式2: 采用基本的流,一个多个字节的方式赋值
            //method2("d:\test.avi", "d:\copy2.avi");
            //方式3: 采用高效的流,一次一个字节的方式复制
            //method3("d:\test.avi", "d:\copy3.avi");
            //方式4: 采用高效的流,一个多个字节的方式赋值
            method4("d:\test.avi", "d:\copy4.avi");
            
            //结束计时
            long end = System.currentTimeMillis();
            //打印耗时多少毫秒
            System.out.println("共耗时 " +(end - start)+ "毫秒");
        }
        
        //方式4: 采用高效的流,一个多个字节的方式赋值
        private static void method4(String src, String dest) throws IOException {
            //1,指定数据源
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
             //2,指定目的地
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
             //3,读数据
            byte[] buffer = new byte[1024];
            int len = -1;
            while ( (len = in.read(buffer)) != -1) {
                //4,写数据
                out.write(buffer, 0, le n);
            }
             //5,关闭流
            in.close();
            out.close();
        }
    
        //方式3: 采用高效的流,一次一个字节的方式复制
        private static void method3(String src, String dest) throws IOException {
            //1,指定数据源
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
             //2,指定目的地
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
             //3,读数据
            int ch = -1;
            while ((ch=in.read()) != -1) {
                //4,写数据
                out.write(ch);    
            }        
             //5,关闭流
            in.close();
            out.close();
        }
    
        //方式2: 采用基本的流,一个多个字节的方式赋值
        private static void method2(String src, String dest) throws IOException {
            //1,指定数据源
            FileInputStream in = new FileInputStream(src);
            //2,指定目的地
            FileOutputStream out = new FileOutputStream(dest);
            //3,读数据
            byte[] buffer = new byte[1024];
            int len = -1;
            while ( (len=in.read(buffer)) != -1) {
                //4,写数据
                out.write(buffer, 0, len);
            }
            //5,关闭流
            in.close();
            out.close();
        }
    
        //方式1: 采用基本的流,一次一个字节的方式复制
        private static void method1(String src, String dest) throws IOException {
            //1,指定数据源
            FileInputStream in = new FileInputStream(src);
            //2,指定目的地
            FileOutputStream out = new FileOutputStream(dest);
            //3,读数据
            int ch = -1;
            while (( ch=in.read()) != -1) {
                //4,写数据
                out.write(ch);
            }
            //5,关闭流
            in.close();
            out.close();
        }
    }

    字符缓冲流

     字符缓冲输入流 BufferedReader

     字符缓冲输出流 BufferedWriter

    /*
     * BufferedWriter 字符缓冲输出流
     * 方法
     *     public void newLine()写入一个行分隔符
     * 
     * 需求: 通过缓冲输出流写入数据到文件
     * 分析:
     *     1,创建流对象
     *     2,写数据
     *     3,关闭流
     * 
     */
    public class BufferedWriterDemo {
        public static void main(String[] args) throws IOException {
            //创建流
            //基本字符输出流
            FileWriter fileOut = new FileWriter("file.txt");
            //把基本的流进行包装
            BufferedWriter out = new BufferedWriter(fileOut);
            //2,写数据
            for (int i=0; i<5; i++) {
                out.write("hello");
                out.newLine();
            }
            //3,关闭流
            out.close();
        }
    }
    /*
     * BufferedReader 字符缓冲输入流
     * 
     * 方法:
     *     String readLine() 
     * 需求:从文件中读取数据,并显示数据
     */
    public class BufferedReaderDemo {
        public static void main(String[] args) throws IOException {
            //1,创建流
            BufferedReader in = new BufferedReader(new FileReader("file.txt"));
            //2,读数据
            //一次一个字符
            //一次一个字符数组
            //一次读取文本中一行的字符串内容
            String line = null;
            while( (line = in.readLine()) != null ){
                System.out.println(line);
            }
            
            //3,关闭流
            in.close();
        }
    }

    使用字符缓冲流完成文本文件的复制

    /*
     * 采用高效的字符缓冲流,完成文本文件的赋值
     * 
     * 数据源: file.txt
     * 目的地: copyFile.txt
     * 
     * 分析:
     *     1,指定数据源, 是数据源中读数据,采用输入流
     *     2,指定目的地,是把数据写入目的地,采用输出流
     *     3,读数据
     *     4,写数据
     *     5,关闭流
     */
    public class CopyTextFile {
        public static void main(String[] args) throws IOException {
            //1,指定数据源, 是数据源中读数据,采用输入流
            BufferedReader in = new BufferedReader(new FileReader("file.txt"));
            //2,指定目的地,是把数据写入目的地,采用输出流
            BufferedWriter out = new BufferedWriter(new FileWriter("copyFile.txt"));
            //3,读数据
            String line = null;
            while ( (line = in.readLine()) != null ) {
                //4,写数据
                out.write(line);
                //写入换行符号
                out.newLine();
    Out.flush();
            }
            //5,关闭流
            out.close();
            in.close();
        }
    }

     流的操作规律

     明确一:要操作的数据是数据源还是数据目的。

                      源:InputStream    Reader

                      目的:OutputStream Writer

    先根据需求明确要读,还是要写。

    明确二:要操作的数据是字节还是文本呢?

                      源:

                              字节:InputStream

                              文本:Reader

                      目的:

                              字节:OutputStream

                              文本:Writer

    已经明确到了具体的体系上。

    明确三:明确数据所在的具体设备。

                      源设备:

                              硬盘:文件  File开头。

                              内存:数组,字符串。

                               键盘:System.in;

                              网络:Socket

                      目的设备:

                              硬盘:文件  File开头。

                              内存:数组,字符串。

                              屏幕:System.out

                              网络:Socket

    完全可以明确具体要使用哪个流对象。

    明确四:是否需要额外功能呢?

                      额外功能:

                              转换吗?转换流。InputStreamReader OutputStreamWriter

                              高效吗?缓冲区对象。BufferedXXX

         InputStream

           FileInputStream

           BufferedInputStream

    OuputStream

       FileOutputStream

       BufferedOuputStream

    Writer

      OutputStreamWriter

         FileWriter

          BufferedWriter

    Reader

      InputStreamReader

         FileReader

     BufferedReader

     

  • 相关阅读:
    软件开发面试
    jQuery插件
    基于消息的软件架构
    线程池的原理及实现(转)
    java实现生产者消费者问题(转)
    并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法(转)
    JAVA CAS原理深度分析(转)
    菜鸟nginx源码剖析 框架篇(一) 从main函数看nginx启动流程(转)
    Android中利用Handler实现消息的分发机制(三)
    char* 和char[]的差别
  • 原文地址:https://www.cnblogs.com/haoduoyu0512/p/13425255.html
Copyright © 2011-2022 走看看