简体   繁体   English

与Java中的.txt文件相比,将集合读/写到.ser文件

[英]reading/writing a set to a .ser file compared to a .txt file in java

So I have this Java program, and at the moment it serializes my set object and saves it to a .ser file 所以我有了这个Java程序,此刻它序列化了我的set对象并将其保存到.ser文件中

 FileOutputStream fileOut = new FileOutputStream("hash.ser");
          ObjectOutputStream out = new ObjectOutputStream(fileOut);
          out.writeObject(mySet);
          out.close();
          fileOut.close();
          System.out.printf("Serialized data is saved in hash.ser");

The program also reads from the .ser file at start of program each time. 程序每次在程序启动时也会从.ser文件读取。 I was just curious if someone can explain the differences in both speed, and how it works in general between reading/loading from a txt file compared to a .ser file. 我只是很好奇,是否有人可以解释两种速度的差异,以及从txt文件与.ser文件进行读取/加载之间的一般速度。 As I am not 100% sure it is actually faster to read/write to the .ser file like I did. 因为我不是100%肯定像我一样读/写.ser文件实际上更快。 Looked into this a bit and really couldnt find to much on this. 仔细研究了一下,真的找不到太多。 Any help would be great, thanks. 任何帮助将是巨大的,谢谢。

It's impossible to compare Java serialization to a "txt" file because the text file could be created and read in any number of different ways. 将Java序列化与“ txt”文件进行比较是不可能的,因为可以用许多不同的方式创建和读​​取文本文件。 For example, you could write XML, or JSON, or your own custom text format to a text file. 例如,您可以将XML或JSON或您自己的自定义文本格式写入文本文件。 These are all quite different and will potentially vary considerably in their performance and other characteristics. 这些都有很大的不同,它们的性能和其他特征可能会有很大的不同。

Java serialization can be convenient under many circumstances, but there are also a lot of restrictions and other considerations that can make things complicated quite quickly. Java序列化在许多情况下都可以很方便,但是也有很多限制和其他考虑因素会使事情变得非常复杂。

  • To use serialization successfully, everything you serialize, and everything it points to, must be serializable. 要成功使用序列化,序列化的所有内容及其指向的所有内容都必须可序列化。 Primitives, strings, and core collections ( ArrayList , HashSet , etc.) are all serializable. 基本体,字符串和核心集合( ArrayListHashSet等)都可以序列化。 If you include something that isn't serializable, you have to declare it transient and deal with its absence upon deserialization, or you have to create a custom serialization format. 如果包含无法序列化的内容,则必须声明它为transient并在反序列化时解决它的缺失,或者必须创建自定义序列化格式。 This adds complexity. 这增加了复杂性。
  • Serialization preserves the object graph. 序列化保留对象图。 Suppose your set contains obj1 and obj2 , each of which has a reference to the same object obj3 . 假设您的集合包含obj1obj2 ,它们每个都有对相同对象obj3的引用。 Serializing and deserializing this structure will preserve this relationship, so afterwards both obj1 and obj2 will point to the same obj3 instance. 对该结构进行序列化和反序列化将保留此关系,因此之后obj1obj2都指向同一obj3实例。 Other serialization mechanisms might not do this, and you'd end up with some object pointing to a copy of obj3 . 其他序列化机制可能无法做到这一点,最终您会得到一些指向obj3副本的obj3
  • Serialization can be brittle. 序列化可能很脆弱。 Unless deserialization is done on a system with the exact same classes that were used for serialization, you'll get serialization compatibility exceptions. 除非在具有与序列化完全相同的类的系统上完成反序列化,否则您将获得序列化兼容性异常。 The way to prevent this is to declare serialVersionUID in all of your classes, but then you might have to deal with evolution of this class, for example, if serialized fields are added or removed. 防止这种情况的方法是在所有类中声明serialVersionUID ,但是例如,如果添加或删除了序列化的字段,则可能必须处理此类的演变。 Again, this adds complexity. 同样,这增加了复杂性。

As for performance, I'd suggest that you try it out and see whether it performs acceptably. 至于性能,建议您尝试一下,看看它的性能是否可以接受。 It might be faster or slower than alternatives, but you'd really have to implement those alternatives and benchmark them and compare the results to Java serialization. 它可能比替代方案更快或更慢,但是您实际上必须实现这些替代方案并对其进行基准测试,并将结果与​​Java序列化进行比较。

I assume you are asking the difference between serialization and File IO to write the object state to the file. 我假设您正在问序列化和文件IO之间的区别,以将对象状态写入文件。

The primary use of Serialization is to send the object state over network to the other VM as in RMI. 串行化的主要用途是像RMI中一样通过网络将对象状态发送到另一个VM。 Writing object state to file using serialization is used rarely in production code. 在生产代码中很少使用序列化将对象状态写入文件。

If you are using File IO to write the object state to file , you have to manually take care of writing all the fields to the file including the transient fields . 如果使用文件IO将对象状态写入文件,则必须手动注意将所有字段(包括瞬态字段)写入文件。

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

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