• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please explain the working of read() function

 
Anchit Jindal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is written on the websites that read() function (without any arguments)of FileReader class reads a file character by character. But when I executed the following loop it returned the correct size of the file in bytes whereas according to definition of read() function it should return the half of the size of file in bytes. Why is it so?

while((c=fr.read())!=-1)
{
count++;
fw.write(c);
}
int bytes=count;
System.out.println(bytes);

Please explain
Thanks a lot....

Anchit Jindal
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It all depends on the file's encoding. With an ASCII encoding, each character is stored in one byte, and the file size in bytes is the same as the number of characters. With a UTF-16 encoding, each character is stored in two bytes, and the file size in bytes is twice as large as the number of characters.
 
Anchit Jindal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot man....Could you please provide me a link where detailed information on file encoding is given.......
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Character encoding. In this context it's converting between streams of characters to streams of bytes and vice versa.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!