我试图制作一个相当简单的Android项目,并且需要围绕平台上的数据存储。我发现一些代码似乎做了类似于我想做的事情。但是,我很好奇' readObject'函数,如果尚未将任何对象写入文件。它会返回null吗?
public final class InternalStorage{
private InternalStorage() {}
public static void writeObject(Context context, String key, Object object) throws IOException {
FileOutputStream fos = context.openFileOutput(key, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.close();
fos.close();
}
public static Object readObject(Context context, String key) throws IOException,
ClassNotFoundException {
FileInputStream fis = context.openFileInput(key);
ObjectInputStream ois = new ObjectInputStream(fis);
Object object = ois.readObject();
return object;
}
}
答案 0 :(得分:1)
如果文件长度为零,它将在EOFException,
的构造函数中抛出ObjectInputStream
,如果已写入对象流头,则在readObject()
中抛出ObjectOutputStream
(即writeObject(null),
的构造函数{1}}已执行。)
它会返回null吗?
没有。如果写 null,它将返回null,例如使用writeObject(object)
或object
{{1}}为空,而不是其他任何时间。