ตัวอย่าง
package demo.file;
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 WriteUTF8EncodedExample {
/**
* @param args
*/
public static void main(String[] args) {
try {
File fileDir = new File("C:\\users\\nopphanan7\\WriteUTF-8-file.txt");
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir), "UTF8"));
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());
}
}
}
ผลลัพธ์ที่ได้คือหมายเหตุ : หากตัว editor ไม่รองรับ utf-8 ให้ตั้งค่า โดยไปที่ Window -> Preferences -> General -> Workspace : Text file encoding : utf-8 นะครับ

0 comments:
Post a Comment