毕向东_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();
        }
    }
}

 

posted @ 2017-02-05 23:21  又是火星人  阅读(314)  评论(0编辑  收藏  举报