Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Difference b/w read() and read(char[])

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading a file using FileReader object.


Why size1 is printing 65?, while size2 is showing 11 which is correct.
Is both methods are not same? I think read() is used to read the size of file while read(char[]) is used to read the file in char array and returns the size of file. Am I right? If I am right then why both are showing different values?

Thanks for your considerations.

Regards
Awais
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think read() is used to read the size of file



But what does the API say read() returns?

I was not too lazy to look it up, but I am too lazy to write down what I found
[ September 20, 2006: Message edited by: Barry Gaunt ]
 
author
Posts: 23893
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why size1 is printing 65?, while size2 is showing 11 which is correct.
Is both methods are not same? I think read() is used to read the size of file while read(char[]) is used to read the file in char array and returns the size of file. Am I right? If I am right then why both are showing different values?



read() does *not* return the size of the file. It returns one char that is read (as a int with the unicode value) or -1, if there is nothing to read.

read(char[]) also does *not* return the size of the file. It returns the number of characters that it has read into the array. (and of course, store those values into the array)

See the JavaDoc for more details.

Henry


[UPDATE NOTE: I like Barry's response better. Silly to keep coming here when it takes seconds to look it up]
[ September 20, 2006: Message edited by: Henry Wong ]
 
Awais Sheikh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sir! Thanks for your helpful replies. JavaRanch is always helpful to learners.
Now I understand that
read() returns the single char and read(char[]) returns number of characters in the array which we have read from file.
Thank you very much.

cheers
Awais
 
reply
    Bookmark Topic Watch Topic
  • New Topic