Q
qqq111
Hi all,
I am trying to write a text file from a Java app, so the
file can be correctly viewed by external browsers
(e.g.: IE, EditPlus... ).
I encode the content of the file to UTF-8 and write
it the standard Java way (see code below).
I then open the file with IE, EditPlus...
If the string was in English, it is correctly
displayed - no problems.
If the string was in French/Arabic/Japanese:
I get garbage: '???????'.
Same happens when I switch to
using UTF-16 encoding.
In both case, when I read the output file back into the
original Java app, I find that its content (after decoding)
equals the original string.
I take it to mean that the encryption/decryption
process was successful.
What am I doing wrong?
Thanks,
Gilad
The code:
public void encrypt_decrypt_a_string() throws IOException
{
Charset utf8_cs = Charset.forName("UTF-8"); // or ("UTF-16");
String FILE_PATH = "/tmp/test-file.htm" ;
String STR = "some french or Japanese text here...";
// write to file
//
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(FILE_PATH), utf8_cs);
osw.write( STR);
osw.close();
// read from file
//
InputStreamReader reader = new InputStreamReader(
new FileInputStream(FILE_PATH), utf8_cs);
BufferedReader br = new BufferedReader(reader);
String string = br.readLine();
// verify enc/dec succeeded
//
if( !string.equals(STR))
{
throw new RuntimeException( "enc/dec failure.." );
}
System.out.println("enc/dec success");
}
//EOF
I am trying to write a text file from a Java app, so the
file can be correctly viewed by external browsers
(e.g.: IE, EditPlus... ).
I encode the content of the file to UTF-8 and write
it the standard Java way (see code below).
I then open the file with IE, EditPlus...
If the string was in English, it is correctly
displayed - no problems.
If the string was in French/Arabic/Japanese:
I get garbage: '???????'.
Same happens when I switch to
using UTF-16 encoding.
In both case, when I read the output file back into the
original Java app, I find that its content (after decoding)
equals the original string.
I take it to mean that the encryption/decryption
process was successful.
What am I doing wrong?
Thanks,
Gilad
The code:
public void encrypt_decrypt_a_string() throws IOException
{
Charset utf8_cs = Charset.forName("UTF-8"); // or ("UTF-16");
String FILE_PATH = "/tmp/test-file.htm" ;
String STR = "some french or Japanese text here...";
// write to file
//
OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream(FILE_PATH), utf8_cs);
osw.write( STR);
osw.close();
// read from file
//
InputStreamReader reader = new InputStreamReader(
new FileInputStream(FILE_PATH), utf8_cs);
BufferedReader br = new BufferedReader(reader);
String string = br.readLine();
// verify enc/dec succeeded
//
if( !string.equals(STR))
{
throw new RuntimeException( "enc/dec failure.." );
}
System.out.println("enc/dec success");
}
//EOF