aspose file tools*
The moose likes Beginning Java and the fly likes readLine method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "readLine method" Watch "readLine method" New topic
Author

readLine method

swapna marepally
Greenhorn

Joined: Apr 04, 2007
Posts: 3
can you please
clarify this bit of code

i wrote code to read data in a file

the following code is reading only first line of file (please clarify why)
import java.io.*;


class File1
{

public static void main(String args[]) throws IOException
{

try
{

File f=new File("bag.txt");
f.createNewFile();
// the fiel will be created in the current directory path i.e. in this path itself
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
String data=br.readLine();
System.out.println("the content in the file is");
System.out.println(data);
br.close();

}catch(FileNotFoundException fn)
{
System.out.println(" the file is not found");
}


}


i wanted to read the whole text

i wrote like this

took a while loop

while (data!=null)

but it is going to infinite loop printing the first line

but in text book i found the code like this
and it is perfectly working

String data;
while((data=br.readLine())!=null)

can you please explain it elaborating bit by bit
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The code you posted doesn't have a loop, so it certainly will only process one line.

Show us the code with the loop, and we will see whether we can spot the error.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


Do you know what exactly this means?



At each iteration readLine() must me executed so that it could move forward.
Your previous attempt was just contrary to this. You called readLine() only once, and what you did next in the loop I need not to say; so infinite loop
(value of data was not changing).


Regards,
cmbhatt


cmbhatt
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Since this is the beginner forum, let's take a second to disect that line:

while((data=br.readLine())!=null) {
}

The bit that may be surprising is that an assignment statement like data=br.readLine() is also an expression with a value which happens to be the value that was just given to the left side of the =. So every time through the loop, this reads from the file, assigns the value to the variable data, and checks the value for null. Pretty slick once you get used to the syntax.

This is a very common way to read files or other BufferedReaders. Write it down for future use if you're afraid you might forget.



A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readLine method