aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in IO reader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in IO reader" Watch "problem in IO reader" New topic
Author

problem in IO reader

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
Code:
class test020
{
public static void main(String[] args)
{
try{
File file=new File("\\c:\\check.txt");
PrintWriter pw =new PrintWriter(file);
pw.println("hellooo");
pw.close();
File file1=new File("check.txt");
FileReader fr=new FileReader(file1);
BufferedReader br=new BufferedReader(fr);
System.out.println(br.readLine()); //1
System.out.println(fr.read()); //2

}catch(IOException e)
{
System.out.println("Exception caught");

}

}
}

the output is
hellooo
-1
why cant i read only through fr.read() or br.read()
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
why cant i read only through fr.read() or br.read()

Why can't you? Are you aware that ReadDoesntDoWhatYouThinkItDoes? Of course, not knowing what the file contains, it's impossible to say what the program should be doing.

As an aside, you've been around JavaRanch long enough to know that you should UseCodeTags when posting code any size. It's unnecessarily hard to read as it is.


Ping & DNS - my free Android networking tools app
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Actually, this code shows something very interesting:

To be a little more descriptive, assume that "check.txt" contains a second line, say "Wooorld". Then the result will be the same.

The -1 indicates that the file has been read completely. This is because

1. BufferedReader.readLine() usually reads internally more than just one line
2. The FileWriter instance points internally to the last character that has been read. And reading through the BufferedReader also modifies this position.

So the instances fr and br doesn't read independently from each other.


SCJP 5 (98%) - SCBCD 5 (98%)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
In this code FileReader is wrapped by BufferedReader - BufferedReader reads from FileReader, and FileReader reads from file.
At first read request BufferedReader reads from FileReader a portion of stream (file) into it's buffer in the memory. If file is smaller than the buffer size (probably your file is smaller), then whole file is read at once into the buffer. So, now the whole file was just read from FileReader, and then each call to it's read method returns -1 (end of file). But BufferedReader has it's own file position pointer (that points to the buffer), and each call to the BufferedReader's read method reads next portion of file from buffer.
Ralph Jaus
Ranch Hand

Joined: Apr 27, 2008
Posts: 342
Hi Ireneusz,
each call to the BufferedReader's read method reads next portion of file from buffer
In fact it's a combination of reading from buffer and file, whereby the BufferedReader's position pointer is harmless. It's the FileReader's position pointer that can cause confusion. Look:
will print

aaa
bbbbbbccc

because the second fr.read has positioned fr's position pointer after the second line feed.

But wouldn't Shashank's example give stuff for a nice exam question ?
[ July 06, 2008: Message edited by: Ralph Jaus ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in IO reader