[英]how to read files from Internal Storage
Iam new to android programming this code is giving FilenotFound Exception and going through catch block where should i save my .txt file. 我是android编程的新手,此代码给出了FilenotFound异常,并通过catch块将我的.txt文件保存在哪里。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myCOde=findViewById(R.id.code);
String filename="bully rap.txt";
try {
readCode(filename);
} catch (Exception e) {
myCOde.setText("file not found");
}
}
private void readCode(String filename) throws Exception{
File filesDir=MainActivity.this.getFilesDir();
File myFile=new File(filesDir,filename);
BufferedReader br = new BufferedReader(new FileReader(myFile));
String st;
StringBuilder code= new StringBuilder("hello");
while ((st = br.readLine()) != null){
code.append(st);
myCOde.setText(code);
}
Using below code is not gonna help you to achieve what you want. 使用下面的代码并不能帮助您实现所需的功能。
File filesDir = MainActivity.this.getFilesDir();
Above code usually returns the path where /data/data/{your package name}/files
. 上面的代码通常返回
/data/data/{your package name}/files
的路径。 If you want to access an external file directory, as in you case like Download folder you should use something like below. 如果要访问外部文件目录,例如“ 下载”文件夹,则应使用以下内容。
String filesDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Furthermore if you are going to write to an external file directory, you may need to add neccessary permissions in the manifest.xml 此外,如果要写入外部文件目录,则可能需要在manifest.xml中添加必要的权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.