aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes readLine() and read() functionality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "readLine() and read() functionality" Watch "readLine() and read() functionality" New topic
Author

readLine() and read() functionality

Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Hi,

1> Can you explain why the pointer(reference) associated with readLine method doesn't come again to the first character?
2> What read() mehthod does??



See Dreams With Open Eyes And Make It Real
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
Please reply
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
1)Can you explain why the pointer(reference) associated with readLine method doesn't come again to the first character?
2)What read() mehthod does??


Why you think readLine() should read the same line again and again or the pointer( reference) assiciated with readLine() should come again to the first character.?

Suppose pointer (reference) points to the first character again come to the first character then how will you read more than one line from the file, since you can not change the reference as I dont think there are methods to set the reference or pointer.

Simple answer to your question is because this is how it has been implemented in Java

read() function will read one character at a time and it will return -1 if end of file is met.


Innar Made
Greenhorn

Joined: May 12, 2009
Posts: 17
Since the read() method already reads a single character from the stream, then calling readLine() after that continues reading from the stream where it left off last time. Or in other words, lets imagine that the stream position is 0 before the read() method. Now the read() method executes by reading a single character and stream position is now 1. So calling the readLine() starts reading at the second character.

Check the code below:


The output is:

read() reads character 84
readLine() result is: his is first line


84 in ASCII table is 'T'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: readLine() and read() functionality
 
Similar Threads
Can not find my error ? Help
read file not include new line
FileIO
BufferedWriter, PrinterWriter etc
Function of close() and flush() methods..