0

在我的时区下午好。

我正在使用带有 exJello Provider 的 JavaMail api。我正在使用 SearchTerm 类来过滤检索到的消息,但搜索方法返回结果平均需要 1 分钟以上。所以我决定以这种方式序列化一组消息,我不必等待这么久。所以我有一个理论问题和一个具体问题。1)只有实现 Serializable 接口的类才能被序列化,所以我用来“序列化”这些消息的方式不是“真正的”序列化,对吧?我的代码片段: message.writeTo("OutputStream");

2)现在我正在处理的问题:代码片段:

  messages = inbox.search(new AndTerm(terms));
  ObjectOutputStream stream = new ObjectOutputStream(new  FileOutputStream("serializer.txt"));
   for(Message msg : messages){
      msg.writeTo(stream);
    }

在该过程结束时,我在“serializer.txt”文件中序列化了多条消息。我的问题是如何反序列化这些消息。我已经能够反序列化一条消息,但是如果文件包含多条消息,则只有第一条消息会被反序列化。代码:

ObjectInputStream file = new ObjectInputStream(new FileInputStream("serializer.txt"));
new MimeMessage(session,file);

此代码将仅反序列化一条消息,但如果我制作一条 cicle,则只有第一个将再次反序列化。所以任何机构都面临同样的问题。PS-> 如果我尝试使用任何 InputStream 中的 readObject 方法,它将检索异常,唯一的方法是使用 Message 构造函数。

致以最诚挚的问候

4

2 回答 2

0

You can use either serilizable or externalizable interface for serialization . Serializable is a marker interface and java will do the serialization for you . If you use externalizable you can write your own serialization method .

If we go according to the definition of serialization then what you are doing can be termed serialization . Coz you are writing your object to a file and are able to restore from it.

In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and "resurrected" later in the same or another computer environment.

For the next part :: do not write all serialized objects in the same file . Create a file dynamically for each serialized object. Use a naming structure for identifying the correct file you want to desirialize and that will solve your problem

于 2011-09-07T18:18:11.433 回答
0

你可以试试ObjectInputStream.readObject()

ObjectInputStream messages= new ObjectInputStream(new FileInputStream("serializer.txt"));
while(messages.available()){
   MimeMessage message = (MimeMessage)messages.readObject()
   ....
}
于 2011-09-07T22:30:12.513 回答