• 转换流,缓冲流


     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();
        }

      当我们调用OutputStreamWriter对象的write方法时,会拿着字符到指定的码表中进行查询,把查到的字符编码值转成字节数存放到OutputStreamWriter缓冲区中。然后再调用刷新功能,或者关闭流,或者缓冲区存满后会把缓冲区中的字节数据使用字节流写到指定的文件中。

     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();
        }
    }

    注意:在读取指定的编码的文件时,一定要指定编码格式,否则就会发生解码错误,而发生乱码现象。

    缓冲流

    字节缓冲输出流BufferedOutputStream

    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();
        }
    }

    字节缓冲输入流 BufferedInputStream

    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();
        }
    }

     

     

     

    字符缓冲输出流 BufferedWriter

    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

    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();
        }
    }
  • 相关阅读:
    arduino编程基础之--程序 元素
    arduino编程基础之--环境搭建
    C语言高手之路--目录
    生活中的数据结构
    Manjaro-KDE配置全攻略转
    多线程程序的奇怪问题记录
    manjaro安装openmv ide
    Linux进程数据结构详解
    Linux ps aux指令詳解--转
    记一次粗心大意的代码错误
  • 原文地址:https://www.cnblogs.com/lzw123-/p/9525483.html
Copyright © 2020-2023  润新知