[英]Read and Write from File
I am struggling with how to read and write to a file in Java. 我在如何读写Java文件中苦苦挣扎。
I have the following class which will be written to a file: 我有以下要写入文件的类:
public class EconAppData implements Serializable {
private static final long serialVersionUID = 1432933606399916716L;
protected transient ArrayList<Favorite> favorites;
protected transient List<CatalogTitle> catalogLists;
protected transient int rangeMonthlySettings;
protected transient int rangeQuarterlySettings;
protected transient int rangeAnnualSettings;
EconAppData() {
favorites = new ArrayList<Favorite>();
catalogLists = new ArrayList<CatalogTitle>();
rangeMonthlySettings = 3;
rangeQuarterlySettings = 5;
rangeAnnualSettings = -1;
}
}
This is my read method: 这是我的读取方法:
protected Object readData(String filename) {
Object result;
FileInputStream fis;
ObjectInputStream ois;
try {
fis = openFileInput(filename);
ois = new ObjectInputStream(fis);
result = ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println(filename + " not found");
return null;
} catch (StreamCorruptedException e) {
e.printStackTrace();
System.err.println(filename + " input stream corrupted");
return null;
} catch (IOException e) {
e.printStackTrace();
System.err.println("I/O error in reading " + filename);
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
return result;
}
And the write method: 和写方法:
protected Object writeData(String filename, Object data) {
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = openFileOutput(filename, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(data);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println(filename + " not found");
} catch (StreamCorruptedException e) {
e.printStackTrace();
System.err.println(filename + " output stream corrupted");
} catch (IOException e) {
e.printStackTrace();
System.err.println("I/O error in writing " + filename);
}
return null;
}
The issue : when I am debugging my code, it appears that I am read and writing to my file (so long as the file exists) without hitting any exceptions. 问题 :当我调试代码时,似乎在读写文件(只要文件存在)而没有遇到任何异常。 I read my data and find that EconAppData is not null, however the ArrayLists are null and the ints are 0. I compute these values and write to the file.
我读取了数据,发现EconAppData不为null,但是ArrayLists为null,int为0。我计算这些值并将其写入文件。 I then read the file again (for debugging purposes) and find that all of the data I computed is now gone.
然后,我再次读取该文件(出于调试目的),发现我计算出的所有数据现在都消失了。 Again the EconAppData is not null, however the arraylists are null and the ints are zero.
同样,EconAppData不为null,但是arraylists为null,int为零。
The question : How do I correctly read and write a class that also contains objects to a file? 问题 :如何正确读写还包含对象的类到文件?
Thank you in advance. 先感谢您。
Your variables are all transient which means they don't get saved/loaded. 您的变量都是瞬态的,这意味着它们不会被保存/加载。 Get rid of the transient attributes from all your variables.
摆脱所有变量的瞬时属性。
See: 看到:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.