[英]Reading Objects From A File to An Input Stream
I am presently working on a simple ObjectInputStream
and ObjectOutputStream
, I have read both the documentation and the Java tutorial and am familiar with the basics; 我目前正在研究一个简单的
ObjectInputStream
和ObjectOutputStream
,已经阅读了文档和Java 教程,并熟悉了基础知识。 however, in attempting to compile my program, I am encountering an error that may be related to my misunderstanding of the combination of Map
s and Object input/output , specifically the input portion. 但是,在尝试编译程序时,遇到了一个错误,该错误可能与我对
Map
和对象输入/输出 (特别是输入部分)的组合的误解有关。
I have a .dat file, from which I am trying to read a list of objects that get mapped into a TreeMap
: 我有一个.dat文件,试图从中读取映射到
TreeMap
的对象列表:
public class Product implements Serializable
{
private static final long serialVersionUID = 1L;
private int code;
private String name;
private int quatity;
// Setters and Getters
}
Above, is the code fragment for the Product
object, itself - implementing Serializable
. 上面是
Product
对象本身的代码片段-实现Serializable
。 I include the fragment in case the problem lies, there. 如果出现问题,我会在其中添加片段。
For this question, assume the .dat is not empty and contains properly formatted data. 对于此问题,假定.dat不为空,并且包含格式正确的数据。
Here is my ObjectInputStream
code: 这是我的
ObjectInputStream
代码:
try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file))) {
while (true) {
try {
products = (Map<Integer, Product>) inputStream.readObject();
}
catch (ClassNotFoundException cnfException {
System.out.println("ClassNotFoundException: " + cnfException.getMessage());
}
catch (EOFException eofException) {
System.err.println("EOFException: " + eofException.getMessage());
}
}
When attempting to run this code, I get the following error (a Cast error): 尝试运行此代码时,出现以下错误(强制转换错误):
Here is how I am writing Product
objects to the .dat file: 这是我将
Product
对象写入.dat文件的方式:
try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName))) {
for (int i = 0; i < products.size(); i++) {
outputStream.writeObject(products.get(i));
}
}
Having isolated the error, I know the error occurs when I hit the products =
portion. 隔离出错误后,我知道当我点击
products =
部分时就会发生错误。 I am unsure if this is a compound issue or if this is one of two issues: 我不确定这是复合问题还是这是两个问题之一:
TreeMap
TreeMap
ObjectInputStream
ObjectInputStream
的实现 It sounds like you originally just wrote the Product
objects to an ObjectOutputStream
, rather than a Map<Integer, Product>
. 听起来您最初只是将
Product
对象写入ObjectOutputStream
,而不是Map<Integer, Product>
。 If that's the case, you need something like: 如果是这样,您需要类似以下内容:
Map<Integer, Product> products = new TreeMap<>();
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(file))) {
while (true) {
Product product = (Product) input.readObject();
products.put(product.getCode(), product); // Or whatever
}
} catch (EOFException e) {
// Just finish? Kinda nasty...
}
Of course, that will throw an exception when it reaches the end of the stream - you might want to think about how you're going to detect that cleanly rather than just handling the exception. 当然,这将在流到达末尾时引发异常-您可能需要考虑如何干净地检测到该异常,而不是仅仅处理异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.