* 如何讀取特定編碼格式文件的內容
範例代碼 :
- package gays.encode;
- import java.io.*;
- public class ReadLine {
- /**
- * 簡介 : 以預設編碼進行讀取檔案內容.
- * @param f
- */
- public static void readFile(File f){
- System.out.println("\t***Using" +System.getProperty("file.encoding")+"***");
- try{
- FileReader fr = new FileReader(f);
- BufferedReader br = new BufferedReader(fr);
- String line;
- while((line=br.readLine())!=null){
- System.out.println(line);
- }
- br.close();
- fr.close();
- }catch( FileNotFoundException e){
- e.printStackTrace();
- }catch( IOException ioe){
- ioe.printStackTrace();
- }
- }
- /**
- * 簡介 : 將傳入文件以指定編碼讀入並列印出來.
- * @param f
- */
- public static void readFileEncode(File f,String encode){
- System.out.println("\t***Using "+encode+" encoding***");
- try{
- FileInputStream fis = new FileInputStream(f);
- byte[] lineb = new byte[500];
- int rc = 0;
- StringBuffer sb= new StringBuffer("");
- while((rc = fis.read(lineb))> 0){
- String utf8 = new String(lineb,encode);
- sb.append(utf8+"\n");
- }
- fis.close();
- System.out.println(sb.toString().trim());
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }catch(IOException ioe){
- ioe.printStackTrace();
- }
- }
- public static void main(String args[]){
- File f = new File("G:/aaa/utf_8.txt"); //Read file with UTF-8 encoding.
- File f2 = new File("G:/aaa/big5.txt");
- /*Read file in normal way*/
- System.out.println("\t***Read file with utf-8 encoding***");
- readFile(f);
- System.out.println("\t***Read file with ASCII encoding***");
- readFile(f2);
- System.out.println("===============================================");
- /*Read file with UTF-8 encoding*/
- System.out.println("\t***Read file with utf-8 encoding***");
- readFileEncode(f,"utf-8");
- System.out.println("\t***Read file with ASCII encoding***");
- readFileEncode(f2,"big5");
- }
- }
* 如何讀取特定編碼的內碼
範例代碼 :
- package gays.encode;
- import java.io.UnsupportedEncodingException;
- public class ReadEncodeInHex {
- /**
- * Source : http://www.javaworld.com.tw/jute/post/view?bid=29&id=263997&sty=1&tpg=1&age=0
- * http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=%E5%A5%8E
- * 簡介 : 如何取出字元的編碼(Hex)
- * @param args
- * @throws UnsupportedEncodingException
- */
- public static void main(String[] args) throws UnsupportedEncodingException {
- System.out.println("****a****");
- ReadEncodeInHex.decode("a", "UTF-8");
- ReadEncodeInHex.decode("a", "UTF-16");
- ReadEncodeInHex.decode("a", "BIG5");
- ReadEncodeInHex.decode("a", "GB2312");
- System.out.println("\n****奎****");
- ReadEncodeInHex.decode("奎", "UTF-8");
- ReadEncodeInHex.decode("奎", "UTF-16");
- ReadEncodeInHex.decode("奎", "BIG5");
- ReadEncodeInHex.decode("奎", "GB2312");
- }
- public static void decode(String str, String charSet)
- throws UnsupportedEncodingException {
- byte[] array = str.getBytes(charSet);
- System.out.println("CharSet: " + charSet);
- System.out.println("length: " + array.length);
- System.out.println("values: ");
- for (byte c : array) {
- System.out.print(Integer.toHexString(c & 0xFF)+" ");
- }
- System.out.println();
- }
- }
補充說明 :
@. 你可以參考如下URL得知某個 Char 的各個編碼的內碼
http://www.unicode.org/cgi-bin/GetUnihanData.pl?codepoint=%E5%A5%8E
@. UTF-8 Wiki
@. Unicode Wiki
沒有留言:
張貼留言