This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I/O" Watch "I/O" New topic
Author

I/O

Lancy Mendonca
Ranch Hand

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


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

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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
 
GeeCON Prague 2014
 
subject: I/O