io包之:序列化与反序列化

io包之:序列化与反序列化(缓冲流、对象流、数据流、内存流)

一、缓冲流

1.目的:

为了减少磁盘IO的开销,提高输入流和输出流的性能。

2.BufferedInputStream类(读操作):

例示:

      File f=new File("e:\\1.txt");

      FileInputStream fi=new FileInputStream(f);  //创建流

      BufferedInputStream bis=new BufferedInputStream(fi);

      byte[] bs=new byte[(int)f.length()];

      bis.read(bs);

      System.out.println(new String(bs)); //输出内容

      bis.close();

      fi.close();

3. BufferedOutputStream类(写操作)
例示:

      File f=new File("e:\\1.txt");

      FileOutputStream fos=new FileOutputStream(f);

      BufferedOutputStream bos=new BufferedOutputStream(fos);

      String s="abcd";  //写入的内容

      bos.writer(s.getBytes());  //getBytes()将字符串转成字节数组byte[]

      bos.close();

      fos.close();

二、对象流

对象输入流:ObjectInputStream类

对象输出流:ObjectOutputStream类

三、序列化

1.概念

序列化:将一个对象从内存当中转换到介质(存在硬盘上的介质)的过程。(写的过程)

把内存中的对象转换成介质。是将内存中的对象序列化,保存到文件中或者通过网络传输。

2.思路

(1) 创建个实体类(实现Serializable接口);

(2) 实体化一个对象,并给对象赋值;

(3) 创建对象流,将对象写入文件中;

(4) 关闭资源。

3.代码

//(1)先创建并封装Person类

      //(2)实例化一个对象,并给对象赋值

      Person  ps=new Person();

      ps.setSid(1);

      ps.setSname("谢少文");

      ps.setSex("男");

      ps.setAge(18);

      //(3)创建数据流,并将对象写入文件中

      File f=new File("e:\\2.bak");

      FileOutputStream fos=new FileOutputStream(f);

      BufferedOutputStream bos=new BufferedOutputStream(fos);

      ObjectOutputStream oos=new ObjectOutputStream(bos);

      oos.writeObject(ps);  //将ps对象写入2.bak中(装箱)

      //(4)关闭资源

      oos.close();

      bos.close();

      fos.close();

四、反序列化

1.概念

反序列化:将对象从介质转换为内存里面的对象的过程。(读的过程)也可以看做是释放介质(文件)中的对象到内存。

2.思路

(1) 创建个实体类(实现Serializable接口);

(2) 实体化一个对象,并给对象赋值;

(3) 创建对象流

(4) 关闭资源。

3.代码

实例:

      File f=new File("e:\\imag\\aa.bak");

      FileInputStream fis=new FileInputStream(f);

      BufferedInputStream bis=new BufferedInputStream(fis);

      ObjectInputStream ois=new ObjectInputStream(bis);

      Object o=ois.readObject();  //读取流中的对象

      Person ps=(Person)o;        //强转成实体类(拆箱)

      System.out.println(ps.getSid+"  "+ps.getSname+"  "+ps.getSex+"  "+ps.getAge);

      ois.close();

      bis.close();

      fis.close();

五、数据流

1.数据输入流: DataInputStream类

int read(byte[] b);    //从包含的输入流中读取一定数量的字节,并将它们存储到缓冲区数组 b 中

2.数据输出流: DataOutputStream类

void      write(int a);   //写入数据流中的数据

void      write(byte[] b, int off, int len);

void      flush();   //清空此数据输出流

void      size();   //到目前为止写入此数据输出流的字节数

例如:File f=new File("e:\\temp\\1.txt");

      FileInputStream fis=new FileInputStream(f);

      BufferedInputStream bis=new BufferedInputStream(fis);

      DataInputStream dis=new DataInputStream(bis);

      byte[] by=new byte[1024]; //每次以1024个字节读取

      int n=dis.read(by);

      System.out.println(new String(by));

      dis.close();

      bis.close();

      fis.close();

      ===============================================

      File f=new File("e:\\temp\\cc.txt");

      FileOutputStream fos=new FileOutputStream(f);

      BufferedOutputStream bos=new BufferedOutputStream(fos);

      DataOutputStream dos=new DataOutputStream(bos);

      dos.write(97);

      dos.close();

      bos.close();

      fos.close();

六、内存流

1. ByteArrayInputStream类

int     read();  //从内存输入流中读取下一个字节

2. ByteArrayOutputStream类

void   write(int b);   //写入内存输出流

七、注意:

(1)static修饰的不能被序列化,因为它不属于对象;

(2)transient修饰的也不能被序列化;(transient是Java语言的关键字,用来表示一个域不是该对象串行化的一部分。当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。)

 例: private transient String pwd;

(3)如果一个类的继承体系结构(包含实现的接口)有已经实现或继承了序列化接口的,那么该类自动也能被序列化

猜你喜欢

转载自blog.csdn.net/LYQ2332826438/article/details/81390006