How to write UTF-8 encoded data into a file – Java
Written on April 28, 2009 at 8:16 am by
mkyong
Here’s the Java example to demonstrate how to write UTF-8 encoded data into a text file – “c:\\temp\\test.txt”
P.S Symbol “??” is some “UTF-8″ data in Chinese and Japanese
package com.mkyong; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; public class test { public static void main(String[] args){ try { File fileDir = new File("c:\\temp\\test.txt"); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileDir), "UTF8")); out.append("Website UTF-8").append("\r\n"); out.append("?? UTF-8").append("\r\n"); out.append("??????? UTF-8").append("\r\n"); out.flush(); out.close(); } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } } }
Result
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation



String UTF8Str = new String(s.getBytes(), “UTF8?);
ShouldBE? String UTF8Str = new String(s.getBytes(“UTF8?));
Hello there,
This code does not really work for me.
Here’s how I tested it. My test.txt file is saved with UTF-8 encoding and contains this line:
—————
w été jedn? stron? ôpèç Ûg ütà
—————
My test program below first reads the file in BufferedReader and then writes in Writer.
—————
package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
public class Test_temp {
public Test_temp() {
String sPath = “E:/workspace/project/src/test/test.txt”;
if (sPath != null && !sPath.trim().equals(“”)) {
try {
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sPath + “.new”), “UTF8″));
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(sPath), “UTF8″));
String s = null;
while ((s = in.readLine()) != null) {
String UTF8Str = new String(s.getBytes(), “UTF8″);
System.out.println(“[" + UTF8Str + "]“);
out.append(UTF8Str).append(“\r\n”);
}
System.out.println(“Reading Process Completly Successfully.”);
in.close();
out.flush();
out.close();
} catch (UnsupportedEncodingException ue) {
System.out.println(“Not supported : ” + ue.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
public static void main(String[] args) {
new Test_temp();
}
}
—————
The new generated file (test.txt.new) is also encoded with UTF-8 but characters are corrupted:
———–
?w ?t? jedn? stron? ?p?? ?g ?
———–
Could you please tell me what do I do wrong?
Thanks
ganba
are you sure that you’re using a UTF-( reader to validate the file?
Regards,
Tony
[...] P.S File is created by this article How to write UTF-8 encoded data into a file – Java [...]