Java IO流详解(四)

时间:2015-03-15 18:30:00   收藏:0   阅读:188

Serializable序列化
1 对象的序列化

    class Person implements Serializable {
        private String name;
        private int age;

        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String toString() {
            return "Name:" + this.name + ", Age:" + this.age;
        }
    }

    public class serialTest {
        public static void main(String[] args) {
            String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

            Person p1 = new Person("zhangsan",12);
            Person p2 = new Person("lisi",14);

            //此处创建文件写入流的引用是要给ObjectOutputStream的构造函数玩儿
            FileOutputStream fos = null;
            ObjectOutputStream oos = null;
            try {
                fos = new FileOutputStream(path);
                oos = new ObjectOutputStream(fos);

                //这里可以写入对象,也可以写入其他类型数据
                oos.writeObject(p1);
                oos.writeObject(p2);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

结果:
技术分享

2 反序列化

package com.yin.serialize;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class serialTestFan {
    public static void main(String[] args) {
        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        //好吧,这里代码写得着实有点长了,还要抛异常什么的
        //如果你也看的烦,那就在主方法上抛吧,构造方法里用匿名对象就好了
        //什么?别告诉我你不知道匿名对象
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);

            //这里返回的其实是一个Object类对象
            //因为我们已知它是个Person类对象
            //所以,就地把它给向下转型了
            Person p = (Person)ois.readObject();
            System.out.println(p);
            Person p2 = (Person)ois.readObject();
            System.out.println(p2);
//            Person p3 = (Person)ois.readObject();
//            if (p3 == null) {
//              System.out.print("p3==null");
//          }else {
//              System.out.println(p3);
//          }


            //抛死你,烦烦烦~!!!
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                //还是要记得关闭下流
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

结果:
技术分享
对应于上面序列化时写入的对象。
值得注意的是,如果把注释的代码去掉注释,则出现EOFException异常,也就是说读取完数据对象之后,不允许往下读取了。只能有多少对象数据读取多少对象。

3 管道流
线程的通信

package com.yin.serialize;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

//实现Runnable接口,实现一个读的线程
class Read implements Runnable {
    private PipedInputStream in;
    //将需要读的管道流传入到构造函数中
    public Read(PipedInputStream in) {
        this.in = in;
    }

    //实现读这一线程
    public void run() {
        try {
            byte[] buf = new byte[1024];
            int temp = 0;
            //循环读取
            //read是一个阻塞方法,需要抛异常
            //此处把打印流的代码也加入进来
            //是因为如果没有读取到数据,那么打印的代码也无效
            while((temp = in.read(buf)) != -1) {
                String str = new String(buf,0,temp);
                System.out.println(str);
            }
        } catch (IOException e) {
            //其实这里应抛出一个自定义异常的
            //暂时我还没弄清楚
            e.printStackTrace();
        } finally {
            try {
                //我已经抛火了,这只是为了提醒自己异常很重要
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }  
}

//这里实现一个写的类
class Write implements Runnable {
    private PipedOutputStream out;
    //将管道输入流传进来
    public Write(PipedOutputStream out) {
        this.out = out;
    }

    public void run() {
        try {
            //这里开始写出数据
            out.write("管道操作=========看到没,就是这句!!".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //其实应该可以把这个关闭方法写到上面那个try里边
                //但是这样感觉怪怪的,逻辑不大对
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Demo {
    public static void main(String[] args) {
        PipedInputStream in = new PipedInputStream();
        PipedOutputStream out = new PipedOutputStream();
        try {
            //连接管道
            in.connect(out);

            //创建对象,开启线程
            //此处同样放进try...catch里面
            //因为如果没有链接管道,下面操作无意义
            Read r = new Read(in);
            Write w = new Write(out);
            //把已经实现好run方法的对象放入线程中执行
            new Thread(r).start();
            new Thread(w).start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果:
技术分享
我们知道,字节流和字符流都需要数组来进行流的中转
而管道流则直接串联两条流,一边发送数据,一边接收
然而,同时通信的的两种状态,如何才能确定发送和接收的一致性呢
那么,就需要用到线程,无论是接收方还是发送方先执行
总会造成一个线程的阻塞状态,从而等待另一方的数据传过来
总体而言,管道流的目的,也就是为了线程通信
此外,还有PipedReader和PipedWriter类,操作原理都一样

4 DataOutputStream和DataInputStream类
基本数据类型的写入

package com.yin.serialize;

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataStreamTest {
    public static void main(String[] args) {
        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        DataOutputStream d = null;
            try {
                //此处需要传入一个OutputStream类的对象
                d = new DataOutputStream(new FileOutputStream(path));
                //开始写入基本数据类型
                d.writeInt(12);
                d.writeBoolean(true);
                d.writeDouble(12.2223);
                d.writeChar(97);
                //刷新流
                d.flush();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    d.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }
}

结果:
技术分享

5 基本数据类型的读取

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataReadTest {
    public static void main(String[] args) {
        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        DataInputStream d = null;
            try {
                d = new DataInputStream(new FileInputStream(path));
                //按存储顺序读取基本数据类型
                System.out.println(d.readInt());
                System.out.println(d.readBoolean());
                System.out.println(d.readDouble());
                System.out.println(d.readChar());

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    d.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }
}

结果:
技术分享
这个结果与上面的数据写入顺序一致。也就是说读取数据和写入数据一致!!注意!否则会发生数据的打印错误!
参考 :http://www.cnblogs.com/nerxious/archive/2012/12/17/2822365.html

源码下载

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!