您的位置:首页 > 编程语言 > Java开发

对象流

2016-05-02 16:27 369 查看
对象流的写入

public static void objectWriteStream(File file, Message message ){

  ObjectOutputStream objectOutputStream = null;

  try {

   objectOutputStream = new ObjectOutputStream(new FileOutputStream(file,false));

   objectOutputStream.writeObject(message);

   objectOutputStream.flush();

   

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }finally{

   if(objectOutputStream != null){

    try {

     objectOutputStream.close();

    } catch (IOException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

    }

   }

  }

 }

 

对象流的读取

 public static Message objectReadStream(File file){

  ObjectInputStream objectInputStream = null;

  Message message = null;

  try {

   objectInputStream = new ObjectInputStream(new FileInputStream(file));

   //强转成Object类型。

   message = (Message) objectInputStream.readObject();

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (ClassNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }finally{

   if(objectInputStream != null){

    try {

     objectInputStream.close();

    } catch (IOException e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

    }

   }

  }

  return (message);

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java基础