IO流-13

来源:互联网 发布:订餐管理数据流程图 编辑:程序博客网 时间:2024/06/10 22:06

 


Serializable接口,是标记接口,实现序列化功能

ObjectInputStream和ObjectOutputStream要成对使用,才能保证读写

非静态的不想序列化,加上transient,虽然在堆内存中,但不会被序列化
静态不能序列化,因为静态在方法区中 , 而序列化的对象在堆内存中

 

对象的持久化存储:

在一个文件中写入代码:
import java.io.*;
class ObjectStreamDemo
{
 public static void main(String[] args)throws Exception
 {
  //writeObj();
  readObj();
 }
 
 public static void readObj()throws Exception //读取对象
 {
  ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
  
  Person p =(Person)ois.readObject();//读取对象
  
  System.out.println(p);
  ois.close();
 }
 
 public static void writeObj()throws IOException //写入对象
 {
  //将一个对象写入文件中,是字节流
  ObjectOutputStream oos =
    new ObjectOutputStream(new FileOutputStream("obj.object"));
  
  oos.writeObject(new Person("lisi0", 39, "kr"));
  
  oos.close();  
 }
}

在另一个对象中写入代码:

import java.io.*;
class Person implements Serializable
{
 //自定义类固定标志
 public static final long serialVersionUID = 42L;
 private String name;
 
 transient int age;//非静态的不想序列化,加上transient,虽然在堆内存中,但不会被序列化
 
 static String country = "cn";//静态不能够序列化,因为静态在方法区中
 //而序列化的对象在堆内存中
 
 Person(String name, int age, String country)
 {
  this.name = name;
  this.age = age;
  this.country = country;
 }
 public String toString()
 {
  return name+":"+age+":"+country;
 }
}

--------------------------------------------------------------------------------------------------------------------


管道流:
管道输入流应该连接到管道输出流,管道输入流提供要写入
管道输出流的所有数据字节。通常,数据由某个线程从
PipedInputStream对象读取,并由其他线程将其写入到相应
的PipedOutputStream。不建议对着两个对象尝试使用单个线程,
因为这样可能死锁线程

例:
import java.io.*;
class Read implements Runnable
{
 private PipedInputStream in;
 Read(PipedInputStream in) //构造函数
 {
  this.in = in ; 
 }
 public void run()//覆盖run方法
 {
  try //覆盖run方法,不能抛异常
  {
   byte[] buf = new byte[1024];
   
   System.out.println("读取前没有数据阻塞");
   int len = in.read(buf);//假设Read方法先抢到资源,但是没有数据,则执行Write方法
   System.out.println("读到数据...阻塞结束");
   
   String s = new String(buf, 0, len);
   
   System.out.println(s);
   in.close();
  }
  catch(IOException e)
  {
   throw new RuntimeException("管道读取流失败");
  }
 }
}

class Write implements Runnable
{
 private PipedOutputStream out;
 Write(PipedOutputStream out) 
 {
  this.out = out; 
 }
 public void run()
 {
  try
  {
    System.out.println("开始写入数据,等待6秒后");
    Thread.sleep(6000);//线程暂停6s
    
    out.write("piped lai la".getBytes());
    out.close();
  } 
  catch(Exception e)
  {
   throw new RuntimeException("管道输出流失败"); 
  }
 }
}

class PipedStreamDemo
{
 public static void main(String[] args)throws IOException
 {
  PipedInputStream in = new PipedInputStream();//创建管道流
  PipedOutputStream out = new PipedOutputStream();
  in.connect(out);//输出和输入连接
  
  Read r = new Read(in);
  Write w = new Write(out);
  
  new Thread(r).start();
  new Thread(w).start();
  }
}

 -----------------------------------------------------------------------------------

个人总结:操作对象要使用ObjectInputStream和ObjectOutputStream 创建对象,

被操作的对象要实现Serializable接口,若对象内容不想被访问或修改,可以加上

tranisent或static。管道流要用PipedInputStream和PipedOutputStream创建对象,

注意使用输出流时要用connect连接输入流,使用管道流产生异常要处理。

 

原创粉丝点击