ตัวอย่าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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