如何在Android中读取文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:49:35  来源:igfitidea点击:

How to read text file in Android?

android

提问by sunny

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 :

我将详细信息保存在 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));

Can anyone please help me out to fix this issue ?

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

Regards, Sunny.

问候,阳光。

回答by user1135469

@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.

@hermy 的回答使用dataIO.readLine(),现在已弃用,因此可以在如何在 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.

回答by hermy

You can read a line at a time with this:

您可以一次阅读一行:

FileInputStream fis;
final StringBuffer storedString = new StringBuffer();

try {
    fis = openFileInput("out.txt");
    DataInputStream dataIO = new DataInputStream(fis);
    String strLine = null;

    if ((strLine = dataIO.readLine()) != null) {
        storedString.append(strLine);
    }

    dataIO.close();
    fis.close();
}
catch  (Exception e) {  
}

Change the if to while to read it all.

将 if 更改为 while 以阅读所有内容。

回答by user2964202

Just put your file (ie. named yourfile) inside the res/raw folder (you can create if if doesn't exist) inside your project. The R.raw.yourfile resource will be automatically generated by the sdk. To obtain a String of the text file, just use the code suggested by Vovodroid in the following post: Android read text raw resource file

只需将您的文件(即命名为 yourfile)放在项目中的 res/raw 文件夹中(如果不存在,您可以创建)。R.raw.yourfile 资源将由 sdk 自动生成。要获取文本文件的字符串,只需使用以下帖子中 Vovodroid 建议的代码: Android 读取文本原始资源文件

 String result;
    try {
        Resources res = getResources();
        InputStream in_s = res.openRawResource(R.raw.yourfile);

        byte[] b = new byte[in_s.available()];
        in_s.read(b);
        result = new String(b);
    } catch (Exception e) {
        // e.printStackTrace();
        result = "Error: can't show file.";
    }