常用IO流之处理流

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheidou123/article/details/82962456

一、处理流的原理

处理流使用的是装饰器设计模式,对节点流进行增强。
在这里插入图片描述

处理流四个重点特征:

1.性能提升
2.底层都是节点流
3.释放处理流时会自动释放内部的节点流,如果手动释放,从里到外依次释放
4.缓冲流嵌套一次就行,多次嵌套没用

二、常用缓冲流

1.IO字节缓冲流

⑴BufferedInputStream
 public static void main(String[] args) {
        File src = new File("/Users/caibin/IOstudy/caibin.txt");
        InputStream is = null;
        try {
            //这里直接套上就可以
            is = new BufferedInputStream(new FileInputStream(src));
            //这里的byte一般指定1024*某个数,这里为了效果搞成3
            byte[] cache = new byte[3];
            int len;
            //当读到-1时就到文件末尾了
            while ((len = is.read(cache)) != -1) {
                String str = new String(cache,0,len);
                System.out.println(str);
            }
        } catch (FileNotFoundException ex) {

        } catch (IOException ex) {

        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException ex) {

            }
        }
    }
⑵BufferedOutputStream
public class No51_BufferedOutputStream {
    public static void main(String[] args) {
        //文件不需要存在,不存在的话就创建
        File src = new File("out.txt");
        OutputStream os = null;
        try {
            //这里构造函数有一个是追加的,下面这种默认会覆盖以前的
            os = new BufferedOutputStream(new FileOutputStream(src));
            String msg = "caibin is handsome";
            byte[] cache = msg.getBytes();
            os.write(cache, 0, cache.length);
            //输出流flush一下,保证一个好习惯
            os.flush();
        } catch (FileNotFoundException ex) {

        } catch (IOException ex) {

        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException ex) {

            }
        }
    }
}

2.IO字符缓冲流

不要发生多态~~

⑴BufferedReader
public static void main(String[] args) {
        // 1.创建源
        File src = new File("caibin.txt");
        // 2.选择流
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(src));
            String line = null;
            // 3.操作流,分段读
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        } finally {
            //先打开后关闭
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {

            }
        }
    }
⑵BufferedWriter
public static void main(String[] args) {
        File dest = new File("dest.txt");
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(dest));
            writer.write("handsome");
            writer.newLine();
            writer.write("heidouzi");
            writer.flush();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        } finally {
            //先打开后关闭
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException ex) {

            }
        }
    }

3.IO转换流

⑴作用
字节流转字符流,前提是这个字节流是操作纯文本的
处理过程中可以指定字符集
⑵实例
public class No54_Convert {
    public static void main(String[] args) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));) {
            String msg = "";
            while (!msg.equals("quit")) {
                msg = reader.readLine();
                writer.write(msg);
                writer.newLine();
                writer.flush();
            }
        } catch (Exception ex) {

        }
    }
}

4.IO数据流

方便处理基本类型和string,这里要保证先写先读,不用也要读出来

⑴DataInputStream
⑵DataOutputStream
public class No55_DataStream {
    public static void main(String[] args) throws Exception {
        DataOutputStream dos = null;
        DataInputStream ios = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            dos = new DataOutputStream(new BufferedOutputStream(out));
            dos.writeInt(123);
            dos.writeUTF("黑豆");
            dos.flush();
            byte[] data = out.toByteArray();
            ios = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(data)));
            int a = ios.readInt();
            String b = ios.readUTF();
            System.out.println(a + b);
        } finally {
            try {
                if (ios != null) {
                    ios.close();
                }
            } catch (IOException ex) {

            }
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException ex) {

            }
        }
    }
}

5.IO对象流

对象流可以操作
先写出后读取,保证先写先读,不用也要读出来,对象要实现序列化接口

⑴ObjectInputStream(反序列化)
⑵ObjectOutputStream(序列化)
public class No56_ObjectStream {
    public static void main(String[] args) throws Exception {
        ObjectOutputStream dos = null;
        ObjectInputStream ios = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            dos = new ObjectOutputStream(new BufferedOutputStream(out));
            dos.writeObject(123);
            dos.flush();
            byte[] data = out.toByteArray();
            ios = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(data)));
            Object a = ios.readObject();
            if (a instanceof Integer) {
                System.out.println(a);
            }
        } catch (Exception ex) {
            try {
                if (ios != null) {
                    ios.close();
                }
            } catch (IOException e) {

            }
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {

            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/cheidou123/article/details/82962456