Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

I/O

 
Lancy Mendonca
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am thoroughly confused between FileReader and FileInputStream. From what I understand the former is char oriented stream and the latter is byte oriented. What is the difference between the two.
I wrote a small example
FileWriter fr = new FileWriter("C:/Test/Test.txt");
BufferedWriter br = new BufferedWriter(fr);
for (int i = 0; i< 5; i++)
{
br.write('A');
}
br.close();
Assuming FileReader is char oriented it outputs a char which is 2 bytes in size. However the size of the file is 5
I replaced the FileReader by a FileInputStream and still the size is 5.
Many Thanks
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lancy,
Few text editors currently support Unicode text entry. Perhaps the text editor you used to create the file C:/Test/Test.txt supports only ASCII characters, which are limited to 7 bits.
Remember that FileReader and FileWriter read and write 16-bit characters. However, most native file systems are based on 8-bit bytes. These streams encode the characters as they operate according to the default character-encoding scheme. You can find out the default character-encoding by using System.getProperty("file.encoding"). To specify an encoding other than the default, you should construct an OutputStreamWriter on a FileOutputStream and specify it.
Try writing to a file with a non-default encoding scheme and then see how many bytes are written.
Ajith
 
BWA HA HA HA HA HA HA! Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!