Write unicode char to a file in Java
Description
The following code shows how to write unicode char to a file.
Example
An OutputStreamWriter is a bridge from character streams to byte streams. Characters written to an OutputStreamWriter are encoded into bytes using a specified character set.
//from w ww . j ava2s. c o m
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) {
try {
char[] chars = new char[2];
chars[0] = '\u4F60';
chars[1] = '\u597D';
String encoding = "GB18030";
File textFile = new File("C:\\temp\\myFile.txt");
OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream(textFile), encoding);
writer.write(chars);
writer.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »