Java 在android中逐行读取文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24291721/
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-14 11:20:21  来源:igfitidea点击:

Reading a text file line by line in android

javaandroidjava-io

提问by user3668496

hi i just started learning android development and i'm trying to build an app that reads text from files. i have been searching all over the internet but i don't seem to find the way to do so , so i have a few questions..

嗨,我刚开始学习 android 开发,我正在尝试构建一个从文件中读取文本的应用程序。我一直在互联网上搜索,但我似乎没有找到这样做的方法,所以我有几个问题..

1.how to do this? what is the preferred way to read a file line by line in android?

1.如何做到这一点?在android中逐行读取文件的首选方法是什么?

2.where should i store the file? should it be in the raw folder or maybe in the assets folder?

2.我应该在哪里存储文件?它应该在原始文件夹中还是在资产文件夹中?

so this is what i already tried: " (i think the problem may be with finding the file..)

所以这就是我已经尝试过的:“(我认为问题可能在于找到文件......)

@Override   
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filereader);

    try {
        // open the file for reading
        InputStream fis = new FileInputStream("text.txt");

        // if file the available for reading
        if (fis != null) {

          // prepare the file for reading
          InputStreamReader chapterReader = new InputStreamReader(fis);
          BufferedReader buffreader = new BufferedReader(chapterReader);

          String line;

          // read every line of the file into the line-variable, on line at the time
          do {
             line = buffreader.readLine();
            // do something with the line 
             System.out.println(line);
          } while (line != null);

        }
        } catch (Exception e) {
            // print stack trace.
        } finally {
        // close the file.
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

采纳答案by TyMarc

Depends on what you intend to do with that file. If your goal is only to read the file, then the asset folder is the way to go. If you want to store information in that file when you are done working with it, you should put it on the device.

取决于您打算对该文件做什么。如果您的目标只是读取文件,那么资产文件夹就是您要走的路。如果您想在使用完该文件后将信息存储在该文件中,则应将其放在设备上。

If you choose option number 2, you need to decide if you want other applications to read the file. More information can be found at this address:

如果选择选项 2,则需要决定是否希望其他应用程序读取该文件。更多信息可以在这个地址找到:

http://developer.android.com/training/basics/data-storage/files.html

http://developer.android.com/training/basics/data-storage/files.html

Else, you can read/write directly to the device with the standard java procedure just like you described. Though, the filepath would probably be

否则,您可以使用标准的 java 程序直接读取/写入设备,就像您描述的那样。虽然,文件路径可能是

"/sdcard/text.txt"

“/sdcard/text.txt”

EDIT:

编辑:

Here's some piece of code to get started with

这是一些开始使用的代码

FileInputStream is;
BufferedReader reader;
final File file = new File("/sdcard/text.txt");

if (file.exists()) {
    is = new FileInputStream(file);
    reader = new BufferedReader(new InputStreamReader(is));
    String line = reader.readLine();
    while(line != null){
        Log.d("StackOverflow", line);
        line = reader.readLine();
    }
}

But it assumes that you know you've put the text.txtat the root of your sdcard.

但它假设您知道您已将 放在text.txtSD 卡的根目录下。

If the file is in the assets folder, you have to do this:

如果文件在 assets 文件夹中,则必须执行以下操作:

BufferedReader reader;

try{
    final InputStream file = getAssets().open("text.txt");
    reader = new BufferedReader(new InputStreamReader(file));
    String line = reader.readLine();
    while(line != null){
        Log.d("StackOverflow", line);
        line = reader.readLine();
    }
} catch(IOException ioe){
    ioe.printStackTrace();
}

回答by user1568967

Your code looks good however, you should do your file reading asynchronously. For the file path, it depends if it is a file that you bundle in your APK or a file that you download in the app data folder. Depending on what version of android you are targeting, I would use try with resources...

您的代码看起来不错,但是您应该异步读取文件。对于文件路径,这取决于它是您在 APK 中捆绑的文件还是您在应用程序数据文件夹中下载的文件。根据您所针对的 android 版本,我会尝试使用资源...

to read from assets you can do this in an activity:

要从资产中读取,您可以在活动中执行此操作:

reader = new BufferedReader(
        new InputStreamReader(getAssets().open("filename.txt")));