• 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

readLine() and read() functionality

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please reply
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic