2008年12月2日 星期二

how to read big5 files in Java

  1.  
  2. /*
  3. ReadBig5File.java
  4. read a file in big5 code
  5. > javac ReadBig5File.java
  6. > java ReadBig5File big5.txt
  7. Big5編碼文字檔
  8. */
  9. import java.io.FileInputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.BufferedReader;
  12.  
  13. public class ReadBig5File
  14. {
  15. public static void main(String args[]) throws
  16. java.io.FileNotFoundException,
  17. java.io.UnsupportedEncodingException,
  18. java.io.IOException
  19. {
  20. String file="big5.txt";
  21. FileInputStream fis = new FileInputStream(new File(file));
  22. // java.io.FileNotFoundException
  23. BufferedReader br =new BufferedReader(new InputStreamReader(fis,"BIG5"));
  24. // java.io.UnsupportedEncodingException
  25. // java.io.IOException
  26. while(br.ready())
  27. {
  28. String line=br.readLine();
  29. System.out.println(line);
  30. System.out.flush();
  31. }
  32. br.close();
  33. }
  34. }

沒有留言: