This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

writing unicode to a file  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm writing a program to help me learn the Nigerian language Hausa. It uses lots of non-standard characters (hooked-k etc) and although I can display them in a GUI alright, it all goes wrong when I write and read them to and from a file. Then the non-standard letters just get displayed as ?, so I get lots of words that look like ?ar?ashin, which isn't very helpful. I assumed that since Java uses Unicode internally, it would also use it when writing to a file. Is that wrong, or have I messed up my reader and writer classes?

Writer:


Reader:
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thou shalt never assume!

When you write strings to a stream a character data, by default the unicode characters ar converted to the local representation of chars in the host machine, and these are then written to the stream. When you read a string it converts it back.

Check your java documentation regarding the character sets (java.io) you can set in your writing and reading, otherwise the default is making conversions between Unicode and what ever your machine is using.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably need to create a FileInputStream and specify the exact character set you wish to use. As Jimmy says, FileReader uses the system's default character set. With a little bit of work, you can still create a BufferedReader from the FileInputStream you use. You'll probably need InputStreamReader somewhere in there.

HTH

Layne
 
Jack Lord
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for pointing me in the right direction. In the end I used and similar for the writer class. It seems to work, so it'll do fine until someone points out how terrible it is . I was really pleased with the way my program was coming along while I was just testing the input/output stuff on the console - then I hooked it up to the GUI and got very depressed. Now I'm all optimistic again: so thanks for cheering me up!
 
I'm still in control here. LOOK at this tiny ad!
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!