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 shows an exception and does not update temp1 and temp2. A.

+2
source share
4 answers

The exception you see is that I would strongly recommend a) to catch as a specific type, for example. IOException and b) to register or show with a message or stack trace; and c) at least for checking in LogCat, from the point of view of DDMS, if you program using Eclipse, possibly because Android did not detect config.txt you are trying to open. Typically, for simple cases like yours, files that are closed to the application are opened using openFileInput - see the Documentation for details.

Apart from the exception, your read cycle is faulty: you need to initialize an empty line before entering and fill it in while .

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

However, you do not 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 that your code consumes while , then temp1 and temp2 get null because there is no more text to read.

+7
source
 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(); 
+1
source

br.readLine () when it already consumes a string.

try it

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

if you want to keep the first two lines you have to execute:

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

All Articles