3

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.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
rameezmeans
  • 830
  • 1
  • 10
  • 21

4 Answers4

9

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.

Giulio Piancastelli
  • 15,368
  • 5
  • 42
  • 62
  • I am using this FileInputStream fstream = new openFileInput("config.txt"); but its not working. it can't resolve openFileInput thing. – rameezmeans Oct 28 '11 at 10:44
  • As you see from the documentation linked in my answer, `openFileInput` is a method of `Context`, which `Activity` is derived from. Thus, if you are using the snippet in an activity, `openFileInput` should be already available for you. Otherwise, you need to pass a `Context` instance to the method or class where you want to read the file. – Giulio Piancastelli Oct 28 '11 at 11:09
1
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();
Carnal
  • 21,744
  • 6
  • 60
  • 75
1

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
    }
herrlado
  • 86
  • 1
  • 7
0

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();
}
SERPRO
  • 10,015
  • 8
  • 46
  • 63
  • actually its showing exception because it can't find "config.txt". but if i use "FileInputStream fstream = new openFileInput("config.txt");" then its not recognizing it. – rameezmeans Oct 28 '11 at 10:55
  • Ok. That's another problem then. anyway, that code is what you need to get the first 2 lines of the file (when it does exist). :) – SERPRO Oct 28 '11 at 11:00
  • yeah they exist actually. I saw them separately. – rameezmeans Oct 28 '11 at 11:05