1. 程式人生 > >Eclipse中的原始碼中漢字亂碼的情況解決

Eclipse中的原始碼中漢字亂碼的情況解決

        近日從網上下載了一個仿攜程的原始碼,準備學習。開啟工程後,發現很多中文的註釋都是亂碼,按理來說影響不是太大,但是總歸是看著不爽,於是上網搜尋了很多辦法,來解決這個問題。

        方法一:直接使用windows自帶的文字編輯器,將原始碼另存一下,選定需要編碼的格式,一般選UTF-8,即可。但是缺陷在於,任何一個工程下屬的檔案都非常多,這樣一個一個改起來耗費的時間非常長,只針對於檔案數量比較少的可以採用這種方法。

        方法二:開啟Eclipse後,選中該原始檔,然後右鍵->properties->resource->text file encoding,然後選擇需要格式,但是可選擇的格式很有限,並不能滿足要求。

        方法三:開啟Eclipse後,windows->preferences->general->content types->text->java source->default encoding,輸入原來的編碼方式。這種方法最簡單也是支援格式比較多的方法。

         我在沒有找到方法三的情況下,一怒之下決定自己寫一個轉碼程式。該程式可以自動搜尋給定的路徑下的所有檔案,並將原始的ANSI(GBK)編碼的檔案轉換為UTF-8檔案。程式設計過程中學到的知識包括:1、各種輸入輸出流只是建立一個讀寫的通道,規定這個通道是以什麼方式,什麼編碼來傳輸的。2、換行符在以前的概念裡都是'\n'來表示的,但是在java裡是用"\r\n"來表示的。因此要在讀取到的一行一行的資料中間新增"\r\n",否則列印的時候沒法打印出換行符。程式碼如下圖:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

public class AnsiToUtf8 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc = new Scanner(System.in);
        while(true) {
            System.out.println("請輸入檔案目錄");
            String pathname = sc.nextLine() ;
            if(pathname.equals("ok")) {
                sc.close();
                break;
            }else {
                File file = new File(pathname);
                convertion(file);
            }
        }
    }
    public static int convertion(File file) {
        String filepath = null;//覆蓋原始檔案路徑
        BufferedReader indata = null;
        PrintWriter outdata = null;
        File nextFile = null;
        if(file.isDirectory()) {
            String nextpathname = null;
            String[] filelist = null;
            filelist = file.list();
            
            for(int i=0;i<filelist.length;i++) {
                System.out.println(filelist[i]);
                //接下來遞迴進入下一層檔案目錄
                nextpathname = file.getPath()+'\\'+filelist[i];
                nextFile = new File(nextpathname);
                convertion(nextFile);
            }
            System.out.println(file.getPath());
        }else if(file.isFile()){
            filepath = file.getPath();
            try {
                indata = new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
                String readData = "";
                String temp = null;
                while((temp = indata.readLine())!=null) {
                    readData = readData+"\r\n"+temp ;
                }
                //建立輸出路徑
                outdata =new PrintWriter(new BufferedWriter( new OutputStreamWriter(new FileOutputStream(filepath), "UTF-8")));
                outdata.println(readData);

                indata.close();
                outdata.close();

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.err.println("讀取失敗");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                System.err.println("編碼失敗");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return 0;
        }
        return -1;
    }

}