软件测试学习之路

Java学习笔记 20 - 转换流、缓冲流

2018-08-02  本文已影响2人  乘风破浪的姐姐

本文主要内容
1、转换流
2、缓冲流
3、各种流文件复制方式的效率比较
4、IO流对象的操作规律

01转换流

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

B: OutputStreamWriter写文本文件

C: InputSteamReader读取文本文件

/*
 *  转换流,InputSteamReader读取文本
 *  采用UTF-8编码表,读取文件utf
 */
public static void readUTF()throws IOException{
    //创建自己输入流,传递文本文件
    FileInputStream fis = new FileInputStream("c:\\utf.txt");
    //创建转换流对象,构造方法中,包装字节输入流,同时写编码表名
    InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    char[] ch = new char[1024];
    int len = isr.read(ch);
    System.out.println(new String(ch,0,len));
    isr.close();
}
/*
 *  转换流,InputSteamReader读取文本
 *  采用系统默认编码表,读取GBK文件
 */
public static void readGBK()throws IOException{
    //创建自己输入流,传递文本文件
    FileInputStream fis = new FileInputStream("c:\\gbk.txt");
    //创建转换流对象,构造方法,包装字节输入流
    InputStreamReader isr = new InputStreamReader(fis);
    char[] ch = new char[1024];
    int len = isr.read(ch);
    System.out.println(new String(ch,0,len));
    
    isr.close();
}

D: 转换流子类父类的区别

02缓冲流

A: 缓冲流概述
可提高IO流的读写速度
分为字节缓冲流与字符缓冲流

B: 字节输出流缓冲流BufferedOutputStream
java.io.BufferedOuputStream 作用: 提高原有输出流的写入效率
BufferedOuputStream 继承 OutputStream
方法,写入 write 字节,字节数组

C: 字节输入流缓冲流BufferedInputStream
继承InputStream,标准的字节输入流
读取方法 read() 单个字节,字节数组

D:字符输出流缓冲流BufferedWriter
java.io.BufferedWriter 继承 Writer
写入方法 write () 单个字符,字符数组,字符串

E:字符输入流缓冲流BufferedReader
字符输入流缓冲流BufferedReader读取文本行

F:字符流缓冲区流复制文本文件

        /*
         *  使用缓冲区流对象,复制文本文件
         *  数据源  BufferedReader+FileReader 读取
         *  数据目的 BufferedWriter+FileWriter 写入
         *  读取文本行, 读一行,写一行,写换行
         */
        public class Copy_1 {
            public static void main(String[] args) throws IOException{
                BufferedReader bfr = new BufferedReader(new FileReader("c:\\w.log"));   
                BufferedWriter bfw = new BufferedWriter(new FileWriter("d:\\w.log"));
                //读取文本行, 读一行,写一行,写换行
                String line = null;
                while((line = bfr.readLine())!=null){
                    bfw.write(line);
                    bfw.newLine();
                    bfw.flush();
                }
                bfw.close();
                bfr.close();
            }
        }

03四种文件复制方式的效率比较

        public class Copy {
            public static void main(String[] args)throws IOException {
                long s = System.currentTimeMillis();
                copy_4(new File("c:\\q.exe"), new File("d:\\q.exe"));
                long e = System.currentTimeMillis();
                System.out.println(e-s);
            }
            
            /*
             * 方法,实现文件复制
             *  1. 字节流读写单个字节
             */
            public static void copy_1(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                while((len = fis.read())!=-1){
                    fos.write(len);
                }
                fos.close();
                fis.close();
            }
            
            /*
             * 方法,实现文件复制
             *  2. 字节流读写字节数组
             */
            public static void copy_2(File src,File desc)throws IOException{
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(desc);
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = fis.read(bytes))!=-1){
                    fos.write(bytes,0,len);
                }
                fos.close();
                fis.close();
            }
            
            /*
             * 方法,实现文件复制
             *  3. 字节流缓冲区流读写单个字节
             */
            public static void copy_3(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                while((len = bis.read())!=-1){
                    bos.write(len);
                }
                bos.close();
                bis.close();
            }
            
            /*
             * 方法,实现文件复制
             *  4. 字节流缓冲区流读写字节数组
             */
            public static void copy_4(File src,File desc)throws IOException{
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
                int len = 0 ;
                byte[] bytes = new byte[1024];
                while((len = bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                }
                bos.close();
                bis.close();
            }
        }

04IO流对象的操作规律

05 流的继承关系

java.lang.Object
—— java.io.OutputStream 抽象类,输出字节流的所有类的超类
——> java.io.FileOutputStream 用于写入数据文件原始字节的流
——>>> java.io.FilterOutputStream
——>>>>> java.io.BufferedOutputStream 字节输出流缓冲流

java.lang.Object
—— java.io.InputStream抽象类,字节输入流的所有类的超类
——> java.io.FileInputStream 用于读取数据文件原始字节的流
——>>> java.io.FilterInputStream
——>>>>>java.io.BufferedInputStream字节读取缓冲流

java.lang.Object
—— java.io.Writer 写入字符流的抽象类
——> java.io.OutputStreamWriter
——>>> java.io.FileWriter 用于写入字符流
——> java.io.BufferedWriter 字符输出缓冲流

java.lang.Object
—— java.io.Reader 读取字符流的抽象类
——> java.io.InputStreamReader
—— >>> java.io.FileReader 用于读取字符流
——> java.io.BufferedReader字符输入缓冲流

上一篇 下一篇

猜你喜欢

热点阅读