public class Access {
private static List<Person> list = new ArrayList<Person>();private static List listo = new ArrayList();public static void main(String[] args) {
File file = new File("C:\\x.txt");
FileOutputStream fo = null;
FileInputStream fi = null;
ObjectOutputStream oop = null;
ObjectInputStream ooi = null;
try {
fo = new FileOutputStream(file);
fi = new FileInputStream(file);
try {
oop = new ObjectOutputStream(fo);
ooi = new ObjectInputStream(fi);
try {
//我想在这里取的x.tex里list对象为什么取不到 list = (ArrayList) ooi.readObject();
} catch (EOFException e) {
Person p1 = new Person();
String name = JOptionPane.showInputDialog("请输入名字");
p1.setName(name);
list.add(p1);
oop.writeObject(list);
oop.flush();
for (int i = 0; i < list.size(); i++) {
p1 = list.get(i);
System.out.println(p1.getName());
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fi.close();
fo.close();
oop.close();
ooi.close();
} catch (IOException e) {
e.printStackTrace();
}}}}第一次我输入人的名字是aaa,关闭程序,第2次我输入人的名字是bbb,但是结果不是输出aaa和bbb,只是bbb!
我不是要在文件后追加list对象,我想通过这个一个list集合对象保存Person对象,然后把list对象输出到x.txt,覆盖之前的list对象
我想把第一次x.txt的list对象Read出来,然后用第一次list对象加了第2个人后,在输出x.txt里面,然后遍历这个list对象