如何在Android中读取文本文件? [英] How to read text file in Android?

查看:111
本文介绍了如何在Android中读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在的储蓄中创造了数据/数据​​/ new.android /文件/ out.txt一个文本文件out.txt文件的详细信息。 我能够附加信息的文字,但我无法读取该文件。我用下面的步骤来读取文件:

I am saving details in out.txt file which has created a text file in data/data/new.android/files/out.txt. I am able to append information to the text, however, I am unable to read this file. I used the following procedures to read the file :

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

任何人都可以请帮我解决这个问题?

Can anyone please help me out to fix this issue ?

问候, 阳光。

推荐答案

@ hermy的答案使用 dataIO.readLine(),目前已去precated,所以替代的解决方案,这个问题可以在找到我如何可以读取Android的一个文本文件?。我亲自使用@ SandipArmalPatil的回答......也正是需要。

@hermy's answer uses dataIO.readLine(), which has now deprecated, so alternate solutions to this problem can be found at How can I read a text file in Android?. I personally used @SandipArmalPatil's answer...did exactly as needed.

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.

这篇关于如何在Android中读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆