• 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

FileWriter. From the API.

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can someone help me understand or point me to resources I could read in order to understand the following line from the API description of the class Filewriter ?

"The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable."
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Character encoding. In short, it describes how bytes are converted into characters. The default character encoding is determined by your operating system, and it may not be the actual encoding of the file. If it isn't then FileWriter is not a good option.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a more general level, it means that the FileWriter class abstracts away those two parameters (character encoding and buffer size) - it uses default values that your code can't change. Should you do need to change them, then using an OutputStreamWriter (for the encoding) and a BufferedWriter (for the buffer size) in conjunction with a FileOutputStream are called for. The javadocs of those other classes provide more information about what exactly they do.
 
Tim McMurry
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answers.

I thought I had figured things out after getting your warning about FileWriter and reading the docs, but I am apparently still missing something.

I have a text-file which was likely created on a Windows machine. According to the "file" tool on my linux machine, its encoding is "ISO-8859".

I extract certain strings from this file and write those to a new file. I have done the following, where inFile is the File object for the input file,
and outFile is the File object for the output file.


I write to osw using


Now when I open outFile in a texteditor a questionmark appears for instance where there ought to be a character with an accent egu and checking the type of
outFile with "file" again, shows that it is ASCII despite having chosen "ISO-8859-1" in the OutputStreamWriter constructor .

Hints ?
 
Tim McMurry
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apologies for posing my last question. Hope you havent wasted too much time thinking about it.

This was for a term-project and we were giving code to load some data. On linux machines some bugs occured. This was caused by the provided code using FileWriter. The above code does fix this. I had overseen that the writer was reassigned after initialization apparently for the sole silly purpose of skipping the first line in the file !
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic