【问题标题】:how to read line by line in android?如何在android中逐行读取?
【发布时间】:2026-01-04 17:20:04
【问题描述】:

我正在使用此代码。

try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("config.txt");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          while ((br.readLine()) != null) {
              temp1 = br.readLine();
              temp2 = br.readLine();

          }

          in.close();
    }catch (Exception e){//Catch exception if any
    Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show();
    }
    Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show();

但这显示异常并且没有更新 temp1 和 temp2。

【问题讨论】:

  • 您想将同一行存储在 2 个不同的变量中吗?或者您只想将最后两行保存在 temp1 和 temp2 中?
  • 想要将文件的前两行保存在两个不同的变量中。

标签: java android file-handling


【解决方案1】:

您看到的异常 - 我强烈建议 a) 将其捕获为特定类型,例如IOException,和 b) 记录或显示消息或堆栈跟踪,以及 c) 如果您正在使用 Eclipse 编程,从 DDMS 的角度来看,至少要在 LogCat 中检查 - 可能是由于 Android 找不到 @ 987654323@ 您尝试打开的文件。通常,对于像您这样的最简单的情况,应用程序专用的文件是使用 openFileInput - see the documentation 打开的,以了解详细信息。

除了异常,你的阅读循环有缺陷:你需要在进入前初始化一个空字符串,并填写while条件。

String line = "";
while ((line = br.readLine()) != null) {
    // do something with the line you just read, e.g.
    temp1 = line;
    temp2 = line;
}

但是,如果您只想将前两行保存在不同的变量中,则不需要循环。

String line = "";
if ((line = br.readLine()) != null)
    temp1 = line;
if ((line = br.readLine()) != null)
    temp2 = line;

正如其他人已经指出的那样,调用readLine 会消耗一行,因此如果您的config.txt 文件仅包含一行,那么您的代码会在while 条件下使用它,然后temp1temp2 得到@ 987654333@ 已分配,因为没有更多文字可供阅读。

【讨论】:

  • 我正在使用这个 FileInputStream fstream = new openFileInput("config.txt");但它不工作。它无法解决 openFileInput 的事情。
  • 正如您从我的答案中链接的文档中看到的那样,openFileInputContext 的一种方法,Activity 是从该方法派生的。因此,如果您在活动中使用 sn-p,openFileInput 应该已经可供您使用。否则,您需要将Context 实例传递给您要读取文件的方法或类。
【解决方案2】:

如果你想保存前两行你必须做的:

try
{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("config.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String line = "";
    if((line = br.readLine()) != null)
        temp1 = line;
    if((line = br.readLine()) != null)
        temp2 = line;   
}
catch(Exception e)
{
    e.printStackTrace();
}

【讨论】:

  • 实际上它显示异常,因为它找不到“config.txt”。但如果我使用 "FileInputStream fstream = new openFileInput("config.txt");"然后它不认识它。
  • 好的。那是另一个问题。无论如何,该代码是您获取文件前两行所需的代码(当它确实存在时)。 :)
  • 是的,它们确实存在。我分别看到了它们。
【解决方案3】:

br.readLine() in while 已经消耗了一行。

试试这个

    LineNumberReader reader = new LineNumberReader(new FileReader("config.txt")));
    String line;
    while ((line = reader.readLine()) != null) {
        //doProcessLine
    }

【讨论】:

    【解决方案4】:
    try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("config.txt");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String line = "";
          while ((line = br.readLine()) != null) {
              temp1 = line;
              temp2 = line;
    
          }
    
          in.close();
    }catch (Exception e){//Catch exception if any
    Toast.makeText(getBaseContext(), "Exception", Toast.LENGTH_LONG).show();
    }
    Toast.makeText(getBaseContext(), temp1+temp2, Toast.LENGTH_LONG).show();
    

    【讨论】: