zoukankan      html  css  js  c++  java
  • 毕向东_Java基础视频教程第21天_IO流(1)

    第21天-01-IO流(对象的序列化)

    ObjectInputStream与ObjectOutputStream

    • 被操作的对象需要实现Serializable接口(标记接口)
    • 非必须, 但强烈建议所有可序列化类都显式声明serialVersionUID
    package bxd;
    
    import java.io.*;
    
    public class ObjectStreamDemo {
        public static void readObj() throws Exception {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Person.object"));
            Person person = (Person) ois.readObject();
            System.out.println(person);
            ois.close();
        }
    
        public static void writeObj() throws Exception {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Person.object"));
            oos.writeObject(new Person("lily", 39, "us"));
            oos.close();
        }
    
        public static void main(String[] args) throws Exception {
            // writeObj();
            readObj();
        }
    }
    
    /*
    输出lily:0:cn, 因为age不会被序列化(使用初始值0), 静态变量country也不会被序列化(使用初始值cn).
    */
    class Person implements Serializable {
    
        public static final long serialVersionUID = 42L;  // 强烈建议所有可序列化类都显式声明serialVersionUID
        private String name;
        transient int age;             // 如果某个实例变量不需要被序列化, 可以使用transient修饰
        static String country = "cn";  // 序列化行为只针对Java堆(heap), 而静态变量不存在于heap.
    
        Person(String name, int age, String country) {
            this.name = name;
            this.age = age;
            this.country = country;
        }
    
        public String toString() {
            return name + ":" + age + ":" + country;
        }
    }

     第21天-02-IO流(管道流)

    PipedInputStream和PipedOutputStream

    • 输入和输出可以直接进行连接, 通常结合线程使用.
    • 不建议对这两个对象尝试使用单个线程, 因为这样可能发生线程死锁
    package bxd;
    
    
    import java.io.*;
    
    public class PipedStreamDemo {
    
        public static void main(String[] args) throws IOException {
    
            PipedInputStream pipedInputStream = new PipedInputStream();
            PipedOutputStream pipedOutputStream = new PipedOutputStream();
            // 只要往pipedOutputStream写入的内容, 就可以从pipedInputStream读到
            pipedInputStream.connect(pipedOutputStream);
    
            Read read = new Read(pipedInputStream);
            Write write = new Write(pipedOutputStream);
    
            new Thread(read).start();
            new Thread(write).start();
        }
    }
    
    class Write implements Runnable {
    
        private PipedOutputStream out;
    
        public Write(PipedOutputStream out) {
            this.out = out;
        }
    
        @Override
        public void run() {
            try {
                System.out.println("开始执行PipedOutputStream操作: ");
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    if ("over".equals(line)) break;
                    out.write(line.getBytes());
                    out.write(System.lineSeparator().getBytes());
                }
    
                bufferedReader.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    class Read implements Runnable {
    
        private PipedInputStream in;
    
        public Read(PipedInputStream in) {
            this.in = in;
        }
    
        @Override
        public void run() {
            try {
                System.out.println("开始执行PipedInputStream操作: ");
    
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("读取到的内容.txt"));
    
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = in.read(buf)) != -1) {
                    bufferedOutputStream.write(buf, 0, len);
                }
    
                bufferedOutputStream.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    javaDoc 注释规范
    [阿里云] 如何 开放云主机 非80 端口?
    [Go] 跨平台文件系统监控工具 fsnotify 应用举例
    如何利用 jQuery 修改 css 中带有 !important 的样式属性?
    code.google.com/p/log4go 下载失败
    [Go] ok 判断 汇总
    [Go] 编码规范
    《Go语言实战》摘录:7.3 并发模式
    《Go语言实战》摘录:7.2 并发模式
    《Go语言实战》摘录:7.1 并发模式
  • 原文地址:https://www.cnblogs.com/echo1937/p/6368854.html
Copyright © 2011-2022 走看看