Android 中如何读取文本文件? [复制]

2024-03-01

我将详细信息保存在 out.txt 文件中,该文件已在 data/data/new.android/files/out.txt 中创建了一个文本文件。 我可以将信息附加到文本中,但是我无法读取该文件。我使用以下过程来读取该文件:

File file = new File( activity.getDir("data", Context.MODE_WORLD_READABLE), "new/android/out.txt");
 BufferedReader br = new BufferedReader(new FileReader(file));

谁能帮我解决这个问题吗?

问候, 阳光明媚。


@hermy 的答案使用dataIO.readLine(),现已弃用,因此可以在以下位置找到此问题的替代解决方案如何在 Android 中读取文本文件? https://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android。我个人使用了@SandipArmalPatil 的答案...完全按照需要做了。

StringBuilder text = new StringBuilder();
try {
     File sdcard = Environment.getExternalStorageDirectory();
     File file = new File(sdcard,"testFile.txt");

     BufferedReader br = new BufferedReader(new FileReader(file));  
     String line;   
     while ((line = br.readLine()) != null) {
                text.append(line);
                text.append('\n');
     }
     br.close() ;
 }catch (IOException e) {
    e.printStackTrace();           
 }

TextView tv = (TextView)findViewById(R.id.amount);  
tv.setText(text.toString()); ////Set the text to text view.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android 中如何读取文本文件? [复制] 的相关文章