This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Problem with file and  writing UTF-8 information into file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with file and  writing UTF-8 information into file" Watch "Problem with file and  writing UTF-8 information into file" New topic

Problem with file and writing UTF-8 information into file

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Thank you for reading my post
I have a problem with reading some UTF-8 information from a file and writing it as UTF-8 into another file.
imagine , i have a file that contains some utf-8 text ,(i can read them in windows using notepad/wordpad)
Now i want to write a java application to split the file into some more files and do some changes on them.

here is the method i used to read the file :

and this is how i write the file :

That myChangedText is changed version of String that i read from utf-8 file.

the problem is that information that i see in a.txt are not readable?
even the file signature (EmEditor) shows that it is not a utf file.

i only can read the information if i open the file in OO or MS office , by selecting UTF-8 in select encoding dialog

can some one please help me about this ?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
FileReader and FileWriter assume that you want to use the default character encoding, which in all likelihood is not UTF-8 (that's also stated in their respective javadocs). Use InputStreamReader and OutputStreamWriter to specify the encoding the files are in.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Take care about the Byte Order Mark that some editors and applications will put at the start of a UTF-8-encoded text file, to mark it as such. Java's generally won't recognise this as a Byte Order Mark, but will instead read it as a few junk characters at the start of the file.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Problem with file and writing UTF-8 information into file
It's not a secret anymore!