Reputation: 817
I am trying to write some text to a file using a RandomAccessFile object but the non-english characters are not saved correctly.
Specifically, this sentence --> "und NotenstEnder Libero"
is saved like this --> "und Notenst•nder Libero"
where 'E' character is not english (the ascii code is 917 i think).
The code i am using is this:
file = new RandomAccessFile(path, "rw");
...
file.seek(file.length());
file.writeBytes("The data i want");
How can i avoid this and write the correct text?
(PS: I know about file.writeChars, and i am wondering if there is another way!)
Upvotes: 2
Views: 1006
Reputation: 88707
The main problem might be your file encoding. You should use the correct encoding (probably UTF-8) , e.g.:
byte[] b = "The data i want".getBytes("UTF-8");
file.write(b);
Note that if you're using a text viewer/editor to check the file, depending on which one you're using you might have to write a UTF-8 byte order mark at the beginning of the file or tell the viewer/editor to use UTF-8 if it can't figure it out by itself.
Upvotes: 4
Reputation: 269707
You mean the Greek letter Ε (GREEK CAPITAL LETTER EPSILON) instead of the Latin letter E (LATIN CAPITAL LETTER E)?
In addition to writeChars()
, which would work, there is a writeUTF8()
method that will work as well. Instead of writing two bytes per character, it will use a variable number of bytes, depending on the character code.
Files store bytes. Text is a sequence of characters; in Java, a char
is two bytes. You have to specify one of many, many methods of translating between characters and bytes. Some methods like UTF-8 handle any character, while many others only handle a specific subset of characters, like Latin or Cyrillic. You have to pick a character encoding and then keep track of what you used so that you can decode the file later.
Upvotes: 1
Reputation: 2463
You want your Java code to be UTF-8. If you encode the characters using unicode \unnnn, you can have any unicode characters.
Here's how you would encode your example:
String spanish = "\u00E1\u00E9\u00ED\u00F3\u00FA";
System.out.println(spanish); // prints áéíóú
Upvotes: 0