File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

readLine() and read() functionality

 
Shailesh Phatak
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??

 
Shailesh Phatak
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please reply
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic