aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I/O" Watch "I/O" New topic


Lancy Mendonca
Ranch Hand

Joined: Aug 08, 2000
Posts: 54
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++)
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

Sun Certified Java Programmer<BR>Oracle Certified DBA
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
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.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Consider Paul's rocket mass heater.
subject: I/O
jQuery in Action, 3rd edition