尝试读取保存在手机外部存储器中的文件时,我的代码中出现此错误:
java.io.FileNotFoundException: shopping.txt: open failed: ENOENT (No such file or directory)
我可以成功地将数据写入该文件,这是我很多次所做的。 但是,我无法访问读取同一文件,提供完整路径或通过其他方法访问。
代码编写和保存成功:
File path = new File(this.getFilesDir().getPath());
String value = "vegetables";
// File output = new File(path + File.separator + fileName);
File output = new File(getApplicationContext().getExternalFilesDir(null),"shopping.txt");
try {
FileOutputStream fileout = new FileOutputStream(output.getAbsolutePath());
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(value);
outputWriter.close();
//display file saved message
// Toast.makeText(getBaseContext(), "File saved successfully!",
// Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,String.valueOf(output),Toast.LENGTH_LONG).show();
Log.d("MainActivity", "Chemin fichier = [" + output + "]");
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
编写的代码使我的应用程序崩溃:
try
{
File gFile;
FileInputStream fis = new FileInputStream (new File("shopping.txt"));
//FileInputStream fis = openFileInput("/storage/emulated/0/Android/data/com.example.namour.shoppinglist/files/shopping.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = null, input="";
while ((line = reader.readLine()) != null)
input += line;
Toast.makeText(MainActivity.this,line,Toast.LENGTH_LONG).show();
reader.close();
fis.close();
Toast.makeText(MainActivity.this,"Read successful",Toast.LENGTH_LONG).show();
//return input;
}
catch (IOException e)
{
Log.e("Exception", "File read failed: " + e.toString());
//toast("Error loading file: " + ex.getLocalizedMessage());
}
我做错了什么? 当然,这不是权限问题,因为我可以成功写作。
非常感谢您的帮助。
答案 0 :(得分:2)
您未指定正确的路径。您正在当前工作目录中(运行时)寻找名为shopping.txt的文件。
使用正确的路径创建一个新的File对象,它将起作用:
File input = new File(getApplicationContext().getExternalFilesDir(null),"shopping.txt");
。您可以通过编写来重用对象。
答案 1 :(得分:0)
打开文件时,您只是在使用new File("shopping.txt")
。
您需要指定父文件夹,如下所示:
new File(getExternalFilesDir(),"shopping.txt");
答案 2 :(得分:0)
我建议您确保将org.apache.commons.io
用于IO,它们的FileUtils
和FileNameUtils
库很不错。即:FileUtils.writeStringToFile(new File(path), data);
如果要使用它,请添加到gradle中:implementation 'org.apache.commons:commons-collections4:4.1'
关于您的问题。编写文件时,您正在使用:
getApplicationContext().getExternalFilesDir(null),"shopping.txt"
但是在读取文件时,您正在使用:
FileInputStream fis = new FileInputStream (new File("shopping.txt"));
请注意,您并未仅指定文件名指向shopping.txt
的路径。
为什么不做这样的事情:
//Get path to directory of your choice
public String GetStorageDirectoryPath()
{
String envPath = Environment.getExternalStorageDirectory().toString();
String path = FilenameUtils.concat(envPath, "WhateverDirYouWish");
return path;
}
//Concat filename with path
public String GetFilenameFullPath(String fileName){
return FilenameUtils.concat(GetStorageDirectoryPath(), fileName);
}
//Write
String fullFilePath = GetFilenameFullPath("shopping.txt");
FileUtils.writeStringToFile(new File(fullFilePath ), data);
//Read
File file = new File(fullFilePath);
StringBuilder text = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null){
text.append(line);
if(newLine)
text.append(System.getProperty("line.separator"));
}
br.close();