我正在尝试在文件上写一个对象,然后从同一个文件中读取它 我从一个简单的文本文件中填充了一个对象的arraylist。 当我正在尝试写文件
时,我有一个EOFExceptionpublic 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
}
答案 0 :(得分:0)
p = (Persona) ois.readObject();
k++;
while (p != null) {
persone.add(p);
p = (Persona) ois.readObject();
}
您的读取循环无效。 readObject()
在流结束时不返回null,因此将其作为循环终止条件进行测试是没有意义的。
for (;;) {
try {
p = (Persona) ois.readObject();
k++;
persone.add(p);
} catch (EOFException exc) {
break;
}
}