rameezmeans
rameezmeans

Reputation: 850

how to read line by line in android?

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.

Upvotes: 3

Views: 23807

Answers (4)

Giulio Piancastelli
Giulio Piancastelli

Reputation: 15808

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.

Upvotes: 9

SERPRO
SERPRO

Reputation: 10067

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

Upvotes: 0

herrlado
herrlado

Reputation: 86

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
    }

Upvotes: 1

Carnal
Carnal

Reputation: 22064

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

Upvotes: 1

Related Questions