【发布时间】:2017-08-30 20:09:30
【问题描述】:
我正在尝试在文件上写入一个对象,然后从同一个文件中读取它 我从一个简单的文本文件中填充了一个对象数组列表。 当我尝试写入文件时出现 EOFException
public class CaricaTestoScriviOggetti_Copia {
public static void main(String[] args) {
File fileIn = new File("src/Lista.txt");
Scanner input = null;
try {
input = new Scanner(fileIn);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<Persona> persone = new ArrayList<Persona>();
ArrayList<Persona> maggiorenni = new ArrayList<Persona>();
String nome = null;
String cognome = null;
int eta = 0;
while (input.hasNext()) {
nome = input.next();
cognome = input.next();
eta = input.nextInt();
if (input.hasNext("\n"))
input.nextLine();
persone.add(new Persona(nome, cognome, eta));
}
for (Persona p : persone) {
System.out.println(p);
if (p.getEta() > 17)
maggiorenni.add(p);
}
input.close();
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("src/ListaObj.dat");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos = new ObjectOutputStream(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Persona p : maggiorenni) {
try {
oos.writeObject(p);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// END FOR
try {
fos.close();
oos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileOutputStream fos2 = null;
ObjectOutputStream oos2 = null;
try {
fos2 = new FileOutputStream("src/Oggetto.dat");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos2 = new ObjectOutputStream(fos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
oos2.writeObject(maggiorenni.get(1));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos2.close();
oos2.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("src/Oggetto.dat");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois = new ObjectInputStream(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Persona catched = null;
try {
catched = (Persona) ois.readObject();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// END FOR
try {
fis.close();
ois.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("<------------Oggetto Letto------------>");
System.out.println(catched);
// END MAIN
}
// END CLASS
}
【问题讨论】:
-
'我在尝试写入文件时遇到 EOFException。'不,你没有。当您尝试从文件读取时,您有一个
EOFException,原因是文件in 中没有任何内容。注意不要写这样的代码。依赖于先前try块中代码成功的代码必须在该try块内。 -
抱歉,我发布了错误的代码部分。你认为我必须删除这篇文章并用正确的代码部分写一篇文章?
-
这是给我这个错误的代码
-
您应该编辑您的问题。删除错误的代码并输入正确的代码。
标签: java serialization objectinputstream objectoutputstream