How do you write the String to a file? You must be very careful to do that in the equivalent way of how you read it back from the file.
In your case:
OutputStream out = new FileOutputStream(myfile);
out.write(str.getBytes());
out.close();
Then it should work. But you need to keep in mind that str.getBytes()
is not a safe method to use when you write to files, because it uses the platform default encoding for your characters. If you send such a file to some other place and use it there, you may be reading it back with the wrong encoding.
And it's possible that your platform default encoding doesn't even support accented characters! (But if you write and read files in exactly the same way, then you should get exactly the same result, so this wouldn't be the cause of your problem)
The best thing to do is to use the UTF-8
character encoding.
Where ever you used str.getBytes()
, replace it with str.getBytes("UTF-8")
, or str.getBytes(Charset.forName("UTF-8"))
if you want to avoid having to catch UnsupportedEncodingException
[even though every Java implementation is required to support the UTF-8 encoding. It's annoying...]