gpt4 book ai didi

java - 由于奇怪的原因无法读取文件

转载 作者:行者123 更新时间:2023-11-30 03:06:03 25 4
gpt4 key购买 nike

<分区>

我有这些读取和写入文件的方法:

/* Write content to a file */
private void writeToFile(ArrayList<String> list) {
File file = new File("jokesBody1.bjk");
FileOutputStream fos;
if(list != null){
try {
file.createNewFile();
fos = openFileOutput("jokesBody1.bjk",Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(list);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
file.createNewFile();
fos = openFileOutput("jokesBody1.bjk",Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject("");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

/* Read file's content */
private ArrayList<String> readFromFile() {
File file = new File("jokesBody1.bjk");
ArrayList<String> list = new ArrayList<String>();
try {
file.createNewFile();
ObjectInputStream ois = new ObjectInputStream( new FileInputStream( file ) );
try {
list = (ArrayList)ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
ois.close();
} catch (IOException e) {
Log.e("log activity", "Can not read file: " + e.toString());
}

return list;
}

Everyrhing 对我来说似乎没问题,但是当我运行代码时出现以下错误:

02-15 17:02:07.655: E/log activity(1882): Can not read file: java.io.IOException: open failed: EROFS (Read-only file system)

为什么系统是只读?如果 file.createNewFile(); 不存在文件,我在创建文件时应该做些什么吗?

我知道我遗漏了一些非常小的东西,但作为一个初学者,我无法发现它。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com