为什么我不能读取的只读文件? [英] Why I can't read a read only file?
问题描述
我有这样的方法应该读取文件:
/ *读取文件的内容* /
私人的ArrayList<字符串> readFromFile(){
档案文件=新的文件(jokesBody1.bjk);
ArrayList的<字符串>名单=新的ArrayList<字符串>();
尝试 {
file.createNewFile();
ObjectInputStream的OIS =新的ObjectInputStream(新的FileInputStream(文件));
尝试 {
名单=(ArrayList中)ois.readObject();
}赶上(ClassNotFoundException异常E){
e.printStackTrace();
}
ois.close();
}赶上(IOException异常E){
Log.e(日志活动,无法读取文件:+ e.toString());
}
返回列表;
}
当我叫它,它返回:
6月二号至16号:15:32.686:E /日志活动(1380):无法读取文件:java.io.IOException异常:打开失败:EROFS(只读文件系统)
甚至,如果该文件是只读的,我为什么不能看呢?我真的不明白什么是wroong。我有这个premission在我的清单:
<使用-权限的Android:名称=android.permission.READ_EXTERNAL_STORAGE/>
有人可以给我一个线索?我知道我失去了一些东西小,但我真的无法发现它。
下面是我写的文件:
/ *内容写入到文件* /
私人无效将writeToFile(ArrayList中<字符串>清单,上下文续){
档案文件=新的文件(jokesBody1.bjk);
FileOutputStream中FOS;
如果(名单!= NULL){
尝试 {
FOS = cont.openFileOutput(jokesBody1.bjk,Context.MODE_PRIVATE);
ObjectOutputStream的OUT =新的ObjectOutputStream(FOS);
out.writeObject(名单);
out.close();
}赶上(FileNotFoundException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
}其他{
尝试 {
file.createNewFile();
FOS = openFileOutput(jokesBody1.bjk,Context.MODE_PRIVATE);
ObjectOutputStream的OUT =新的ObjectOutputStream(FOS);
out.writeObject();
out.close();
}赶上(FileNotFoundException异常E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
}
}
您要创建文件,这当然无法在只读文件。
删除此行:
file.createNewFile();
这是通常用来写入之前创建一个新的空文件。你真的不需要它,如果你只是想读取已存在的文件。
编辑:
只需使用这样的:
ObjectInputStream的OIS =新的ObjectInputStream(context.openFileInput(jokesBody1.bjk));
当然,你也必须通过上下文的功能。
您只能使用文件
用完整路径。为了访问您的私人文件,使用右键,就如同您在保存文件的时候做的。
您全功能应该是这样的:
私人的ArrayList<字符串> readFromFile(上下文的背景下){
ArrayList的<字符串>名单=新的ArrayList<字符串>();
尝试 {
ObjectInputStream的OIS =新的ObjectInputStream(context.openFileInput(jokesBody1.bjk));
尝试 {
名单=(ArrayList中)ois.readObject();
}赶上(ClassNotFoundException异常E){
e.printStackTrace();
}
ois.close();
}赶上(IOException异常E){
Log.e(日志活动,无法读取文件:+ e.toString());
}
返回列表;
}
I have this method supposed to read a file:
/* 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;
}
When I call it, it returns:
02-16 06:15:32.686: E/log activity(1380): Can not read file: java.io.IOException: open failed: EROFS (Read-only file system)
Even, if the file is read only, why I can't read it? I really can't understand what is wroong. I have this premission in my manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Can someone give me a clue? I know that I'm missing something small, but I really can't spot it.
Here is how I write the file:
/* Write content to a file */
private void writeToFile(ArrayList<String> list, Context cont) {
File file = new File("jokesBody1.bjk");
FileOutputStream fos;
if(list != null){
try {
fos = cont.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();
}
}
}
You are trying to create the file, which of course fails on a read-only file.
Remove this line:
file.createNewFile();
This is usually used to create a new empty file before writing to it. You really don't need it if you just want to read a file that already exists.
EDIT:
Just use this:
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesBody1.bjk"));
Of course, you'll also have to pass a Context to the function.
You can only use File
with a full path. For accessing your private files, use Context, just as you do when saving the file.
Your full function should look like:
private ArrayList<String> readFromFile(Context context) {
ArrayList<String> list = new ArrayList<String>();
try {
ObjectInputStream ois = new ObjectInputStream( context.openFileInput("jokesBody1.bjk"));
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;
}
这篇关于为什么我不能读取的只读文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!