• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I/O

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic