java - BufferedReader readLine() and moving through files -


my question relates how readline() affects current position within file.

let's have file has following in it:

1

if

r.readline() 

then return "1"

if another

r.readline() 

it return 'null' there 1 line.

so - if check line in while statement so:

while (r.readline!=null){ //do here 

}

so should run until hits null line exit.

however, if want like:

string line;  if ((line = r.readline()).equals("1")) //then something.... else if ((line = r.readline()).equals("2")) //then else 

what happens obviously,by time 2nd check, read position has moved next line.

i tried doing this:

string line = r.readline(); if (line=='1') //do else if (line=="2") //do else. 

...however, bizarre results. if can confirm system.out.println command string 'line' equal say, 1 (or whatever value). when try use in if statement, condition never seems trigger....

any here appreciated.

if (line=='1') //do else if (line=="2") //do else. 

== used comparing references or primitive types, can test string equality using equals():

string x = "hello"; string y = "hello"; 

now x == y true, becuase refer same object in string pool

string x = "hello"; string y = new string("hello"); 

now x == y false, because not pointing same object anymore, 1 in string pool, while other not.

to test use equals(string):

string x = "hello"; string y = new string("hello");  x.equals(y); 

will evaluate true


Comments