简体   繁体   English

如何使用缓冲区读取和写入Java对象

[英]how to use buffer to read and write java objects

I need to write and read objects to file. 我需要写入和读取对象到文件。 How i can do it with use of buffer? 我如何使用缓冲区? When i use it like that it's only write the last object to the file. 当我那样使用它时,它只会将最后一个对象写入文件。

        OutputStream file = new FileOutputStream(DRB );
        OutputStream buffer = new BufferedOutputStream( file );
        ObjectOutput out = new ObjectOutputStream( buffer );

        try{
            out.writeObject(e1);
          }
          finally
          {
            buffer.flush();
            out.close();
          }

To append to an ObjectOutputStream there is only two options as I see it 要追加到ObjectOutputStream,只有两个选项,如我所见

  • read all the data into a list, add the item and write all the objects. 将所有数据读取到列表中,添加项目并写入所有对象。 An ObjectStream is a single continous stream. ObjectStream是单个连续流。 It is not like text where you can keep adding to the end. 它不像文本,您可以继续添加到末尾。
  • use your own format to write multiple independent stream to the same file. 使用您自己的格式将多个独立的流写入同一文件。 You can write to a ByteArrayOutputStream, and use this to write the length of the stream before writing the contents. 您可以写入ByteArrayOutputStream,并在写入内容之前使用它来写入流的长度。 This way you can read the individual stream back in. I would only do this if you are confident in processing binary files. 这样,您可以读回单个流。只有在您有信心处理二进制文件时,我才这样做。

The object you used e1 must be serialized class. 您使用的e1对象必须是序列化的类。

That's the only criteria. 那是唯一的标准。

You can have List of objects you want to write. 您可以具有要编写的对象列表。 Then, you can write this list. 然后,您可以编写此列表。

Please see the code give below. 请参见下面的代码。 Hope this will solve your problem: 希望这能解决您的问题:

package com.shineed.io;

import java.io.FileInputStream;

import java.io.ObjectInputStream;

import java.io.Serializable;

public class Deserializer{

   public static void main (String args[]) {

       Deserializer deserializer = new Deserializer();

       Customer customer = deserializer.deserialzeCustomer();

       System.out.println(customer);
   }

   public Customer deserialzeCustomer(){

       Customer customer;

       try{

           FileInputStream fin = new FileInputStream("c:\\customer.txt");
           ObjectInputStream ois = new ObjectInputStream(fin);
           customer = (Customer) ois.readObject();
           ois.close();

           return customer;

       }catch(Exception e){
           e.printStackTrace();
           return null;
       } 
   } 
}

Also see the Customer class given below: 另请参见下面给出的Customer类:

package com.shineed.io;

import java.io.Serializable;

public class Customer implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private String name;
    private String adress;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAdress() {
        return adress;
    }
    public void setAdress(String adress) {
        this.adress = adress;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM