[英]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序列化在许多情况下都可以很方便,但是也有很多限制和其他考虑因素会使事情变得非常复杂。
ArrayList
, HashSet
, etc.) are all serializable. ArrayList
, HashSet
等)都可以序列化。 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. obj1
and obj2
, each of which has a reference to the same object obj3
. obj1
和obj2
,它们每个都有对相同对象obj3
的引用。 Serializing and deserializing this structure will preserve this relationship, so afterwards both obj1
and obj2
will point to the same obj3
instance. obj1
和obj2
都指向同一obj3
实例。 Other serialization mechanisms might not do this, and you'd end up with some object pointing to a copy of obj3
. obj3
副本的obj3
。 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.