how to read line by line in android?

∥☆過路亽.° 提交于 2019-11-29 23:26:54

问题


i am using this code.

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();

but this is showing exception and is not updating temp1 and temp2.


回答1:


The exception you see - that I would strongly recommend a) to catch as a specific type, e.g. IOException, and b) to log or show with a message or a stack trace, and c) at least to check for in LogCat, from the DDMS perspective if you are programming with Eclipse - is probably due to Android not finding the config.txt file you are trying to open. Usually, for the simplest cases such as yours, files that are private to an application are opened using openFileInput - see the documentation for details.

Apart from the exception, your reading loop is defective: you need to initialize an empty string before entering, and fill it in the while condition.

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

However, you don't need a loop if you just want to save the first two lines in different variables.

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

As others have already pointed out, calling readLine consumes a line, so if your config.txt file contains only one line your code consumes it on the while condition, then temp1 and temp2 get null assigned because there's no more text to read.




回答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 = "";
      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();



回答3:


br.readLine() in while already consumes a line.

Try this

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



回答4:


if you want to save the first two lines you have to do:

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();
}


来源:https://stackoverflow.com/questions/7927855/how-to-read-line-by-line-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!