天天看點

[原創]使用java批量修改檔案編碼(ANSI-->UTF-8)

從網上下載下傳的項目,有時候.java檔案的編碼是ANSI。導入到自己的MyEclipse後,檢視項目源碼的時候,總是亂碼。

一個個.java去修改的話, 既麻煩又不現實。是以寫了下面這個工具類,進行批量轉編碼。

代碼的原理僅僅就是周遊檔案,然後使用流,對按照檔案的原編碼進行讀取,用目的編碼進行寫操作。

直接上源碼:

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {
	// private String sDirectory = "D:/Workspaces/BeyondDiscuz/src/com";
	private String sDirectory = "D:\\Workspaces\\BeyondDiscuz\\src\\com";
	private String dDirectory = "D:\\Workspaces\\BeyondDiscuz\\src\\cn";

	public static void main(String[] args) {
		Main 嗎 = new Main();
		try {
			嗎.readerFile(嗎.sDirectory);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void readerFile(String filePath) throws IOException {
		if ("".equals(filePath) || null == filePath) {
			return;
		}

		File f = new File(filePath);
		if (f.isDirectory()) {
			String[] child = f.list();
			for (int i = 0; i < child.length; i++) {
				String path = f.getAbsolutePath() + File.separator;
				String newPath = path.replace(this.sDirectory, this.dDirectory);
				child[i] = path + child[i];
				File c = new File(child[i]);
				String newFile = child[i].replace(this.sDirectory, this.dDirectory);
				System.out.println("舊路徑:" + path);
				System.out.println("新路徑:" + newPath);

				File newP = new File(newPath);
				if (!newP.exists())
					newP.mkdir();

				if (c.isFile()) {
					System.out.println("舊檔案:" + child[i]);
					System.out.println("新檔案:" + newFile);
					// Charset US-ASCII ISO-8859-1 UTF-8 UTF-16BE UTF-16LE UTF-16  
					BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c), "GBK"));
					// BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c)));
					File newF = new File(newFile);
					newF.createNewFile();
					// BufferedWriter w = new BufferedWriter(new
					// OutputStreamWriter(new FileOutputStream(newF), "UTF-8"));
					BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newF)));
					// BufferedWriter w = new BufferedWriter(new FileWriter(newFile));
					String lineText = null;
					while ((lineText = r.readLine()) != null) {
						// String temp = new String(lineText.getBytes("ISO-8859-1"), "UTF-8");
						w.write(lineText);
						w.newLine();
					}
					w.close();
					r.close();
				} else {
					readerFile(child[i]);
				}
			}
		}
	}

}
      

 上面自己的寫代碼隻是基本思路,下面是更簡潔的代碼:

public static void main(String[] args) {
        try {
            String srcDirPath = "E:\\eclipseWorkspaceSandbase\\test";
            // 轉為UTF-8編碼格式源碼路徑
            String utf8DirPath = "E:\\eclipseWorkspaceSandbase\\test-8";
    
            // 擷取所有java檔案
            Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[] { "java" }, true);
    
            for (File javaGbkFile : javaGbkFileCol) {
                System.out.println(javaGbkFile);
                // UTF8格式檔案路徑
                String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
                // 使用GBK讀取資料,然後用UTF-8寫入資料
                FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }      

作者:

Candyメ奶糖

出處:

http://www.cnblogs.com/Candies/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

博文來源廣泛,如原作者認為我侵犯知識産權,請盡快給我發郵件

[email protected]

聯系,我将以第一時間删除相關内容。