我有一个将列表中的数据写入文件的方法,一个从文件中读取数据到列表中的方法,以及一个将列表中的数据写入文件中指定次数的方法。在使用第一种方法 writeFile ()
之后,我试图从文件中提取数据,一切正常。我通过readFile()
方法将文件中的数据读取到列表中。之后我使用我的方法写入文件所需的次数,一切正常,它写入 multyWrite ()
。但是在那之后我无法从 readFile ()
方法中读取文件中的数据,因为我得到了`
异常堆栈跟踪:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1599)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
at ProductService.readFile(ProductService.java:47)
at Main.main(Main.java:21)
我知道应该使用objectOutputStream.reset()
,但是用在什么地方比较好呢?
private String fileName;
private ProductInterface<FlyingMachine> productService = new ProductInterfaceImpl();
private ObjectOutputStream objectOutputStream;
private FileOutputStream fileOutputStream;
public ProductService(String fileName) throws IOException {
this.fileName = fileName;
fileOutputStream = new FileOutputStream(fileName);
this.objectOutputStream = new ObjectOutputStream(fileOutputStream);
}
public void writeFile() throws IOException {
try {
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
fileOutputStream.close();
}
}
}`
public void readFile() throws IOException {
ObjectInputStream objectInputStream = null;
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileName);
objectInputStream = new ObjectInputStream(fileInputStream);
while (fileInputStream.available() > 0) {
FlyingMachine flyingMachine = (FlyingMachine) objectInputStream.readObject();
productService.getProductContainer().add(flyingMachine);
}
} catch (ClassNotFoundException | EOFException e) {
e.printStackTrace();
} finally {
if (objectInputStream != null) {
objectInputStream.close();
fileInputStream.close();
}
}
}
public void multyWrite(int number) throws IOException {
for (int i = 0; i < number; i++) {
try {
fileOutputStream = new FileOutputStream(fileName, true);
objectOutputStream = new ObjectOutputStream(fileOutputStream);
for (FlyingMachine f : productService.getProductContainer()) {
objectOutputStream.writeObject(f);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (objectOutputStream != null) {
objectOutputStream.flush();
objectOutputStream.close();
}
}
}
}
最佳答案
您在构造函数中创建一个新的 ObjectOutputStream
。在 writeFile
中,您使用该 OOS
实例并关闭它。但在 multyWrite
中,您不使用它而是创建新实例。
现在,当您调用 multyWrite
而没有先调用 writeFile
时,第一个 OOS
仍将打开,但是 OOS
您在 multyWrite
中创建的并不知道这一点 - 因此导致您的文件有两个 OOS
标题。
然后,当您尝试读取此类文件时,ObjectInputStream
将找到第一个 header (一切正常),然后出乎意料地找到第二个 header ,而它需要的是类型代码。该 header 以 0xAC
开头,因此抛出异常消息 “无效类型代码:AC”。
要解决此问题,要么让 multyWrite
使用构造函数中构造的 OOS
,就像 writeFile
一样,要么确保OOS
在您创建新的之前关闭。
在构造函数中打开流(任何类型的)然后依赖外部代码调用特定方法来关闭它通常不是一个好主意。最好在需要时创建流并直接关闭它们。
关于java - 如何避免 java.io.StreamCorruptedException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52017830/