• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in IO reader

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bring out your dead! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic