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.
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 theconfig.txtfile you are trying to open. Usually, for the simplest cases such as yours, files that are private to an application are opened usingopenFileInput– 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
whilecondition.However, you don’t need a loop if you just want to save the first two lines in different variables.
As others have already pointed out, calling
readLineconsumes a line, so if yourconfig.txtfile contains only one line your code consumes it on thewhilecondition, thentemp1andtemp2getnullassigned because there’s no more text to read.