Skip to content

Another Lesson

December 11, 2011

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class UTF8 {

public static void main(String[] args) throws IOException {
String unicode = “\u56CA”;
String anotherUCode = “\u5225”;

byte[] utf8bArray = unicode.getBytes(“UTF-8”);
String finaleString = new String(utf8bArray, “UTF-8”);
byte[] normal = unicode.getBytes();

FileOutputStream myTest = new FileOutputStream(new File(
“c:\\testutf8.txt”));
myTest.write(finaleString.getBytes());
myTest.write(”    “.getBytes());
myTest.write(finaleString.getBytes(“UTF-8”));
myTest.close();
System.out.println(finaleString);

ByteArrayOutputStream baoStream= new ByteArrayOutputStream();
baoStream.write(utf8bArray);
baoStream.write(normal);

byte [] bArray = baoStream.toByteArray();

FileOutputStream baoTest = new FileOutputStream(new File(
“c:\\testutf81.txt”));
baoTest.write(unicode.getBytes());
baoTest.write(”      ====    “.getBytes());
baoTest.write(bArray);
baoTest.write(”      ====    “.getBytes());
baoTest.write(anotherUCode.getBytes());
baoTest.write(”      ====    “.getBytes());
baoTest.write(anotherUCode.getBytes(“UTF-8”));
baoTest.close();

baoStream.close();

}

}

Output

?    囊

?      ====    囊?      ====    ?      ====    別

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: