src路徑下的.java文件用eclipse中打開全是亂碼,但是自己平時使用的是UTF-8字符集,單個修改幾十個源碼文件很累,能否批量修改呢?
-----下面是將工程裏面爲gbk編碼的文件轉爲utf-8的方法,其中
-----String content = convEncoding(value, "gbk", "utf-8"); 是將gbk轉爲utf-8, out.write(content.getBytes("UTF-8")); 最後輸出utf-8
------同理若要從utf-8,z轉成gbk.只需要更改爲 String content = convEncoding(value, "utf-8, "gbk"); out.write(content.getBytes("UTF-8")); 最後輸出 gbk
------具體代碼如下:
package myBean;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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.UnsupportedEncodingException;
import java.text.MessageFormat;
/**
* 該類可以將工程裏面gbk編碼轉爲utf-8的編碼
* @author wb_zjttx
*
*/
public class FileGBK2UTF8 {
public static void main(String[] args) {
// 需要轉換的文件目錄
String fromPath = "D:\\fileTest\\input";
// 轉換到指定的文件目錄
String toPath = "D:\\fileTest\\output";
String [] array={fromPath,toPath};
info("start transform [from path]={0} [to path]={1}", array);
// 遞歸取到所有的文件進行轉換
transform(fromPath, toPath);
}
/**
* 把一個目錄中的文件轉換到另一個目錄中
*
* @param fromPath
* -- 來源文件目錄
* @param toPath
* -- 目標文件目錄
* @return
*/
public static boolean transform(String fromPath, String toPath) {
File ftmp = new File(fromPath);
if (!ftmp.exists()) {
info("轉換文件路徑錯誤!",null);
return false;
}
String [] array={fromPath,toPath};
info("frompath is [{0}], topath is [{1}]", array);
// 如果是文件,則轉換,結束
if (ftmp.isFile()) {
byte[] value = fileToBytes(fromPath);
String content = convEncoding(value, "gbk", "utf-8");
return saveFileUtf8(toPath, content);
} else {
// 查找目錄下面的所有文件與文件夾
File[] childFiles = ftmp.listFiles();
for (int i = 0, n = childFiles.length; i < n; i++) {
File child = childFiles[i];
String childFrom = fromPath + "/" + child.getName();
String childTo = toPath + "/" + child.getName();
transform(childFrom, childTo);
}
}
return true;
}
/**
* 把文件內容保存到指定的文件中,如果指定的文件已存在,則先刪除這個文件, 如果沒有則創建一個新文件,文件內容採用UTF-8編碼方式保存。
* 如果指定的文件路徑不存在,則先創建文件路徑,文件路徑從根目錄開始創建。
*
* @param fileName
* -- 文件路徑
* @param content
* -- 文件內容
* @return
*/
public static boolean saveFileUtf8(String fileName, String content) {
if (fileName == null || fileName.length() == 0)
return false;
if (content == null)
return false;
// 路徑中的\轉換爲/
fileName = fileName.replace('\\', '/');
// 處理文件路徑
createPath(fileName.substring(0, fileName.lastIndexOf('/')));
File file = null;
FileOutputStream out = null;
try {
// 創建或修改文件
file = new File(fileName);
if (file.exists()) {
file.delete();
} else {
file.createNewFile();
}
out = new FileOutputStream(file);
// 添加三個字節標識爲UTF-8格式,也是BOM碼
// out.write(new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF});
out.write(content.getBytes("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
/**
* 把文件內容轉換爲字節數組輸出。
*
* @param fileName
* -- 文件名
* @return
*/
public static byte[] fileToBytes(String fileName) {
FileInputStream ins = null;
ByteArrayOutputStream bos = null;
try {
// 創建文件讀入流
ins = new FileInputStream(new File(fileName));
// 創建目標輸出流
bos = new ByteArrayOutputStream();
// 取流中的數據
int len = 0;
byte[] buf = new byte[256];
while ((len = ins.read(buf, 0, 256)) > -1) {
bos.write(buf, 0, len);
}
// 目標流轉爲字節數組返回到前臺
return bos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ins != null) {
ins.close();
ins = null;
}
if (bos != null) {
bos.close();
bos = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 檢查指定的文件路徑,如果文件路徑不存在,則創建新的路徑, 文件路徑從根目錄開始創建。
*
* @param filePath
* @return
*/
public static boolean createPath(String filePath) {
if (filePath == null || filePath.length() == 0)
return false;
// 路徑中的\轉換爲/
filePath = filePath.replace('\\', '/');
// 處理文件路徑
String[] paths = filePath.split("/");
// 處理文件名中沒有的路徑
/// StringBuilder sbpath = new StringBuilder();
StringBuffer sbpath = new StringBuffer();
for (int i = 0, n = paths.length; i < n; i++) {
sbpath.append(paths[i]);
// 檢查文件路徑如果沒有則創建
File ftmp = new File(sbpath.toString());
if (!ftmp.exists()) {
ftmp.mkdir();
}
sbpath.append("/");
}
return true;
}
/**
* 取路徑中的文件名
*
* @param path
* -- 文件路徑,含文件名
* @return
*/
public static String getFileName(String path) {
if (path == null || path.length() == 0)
return "";
path = path.replaceAll("\\\\", "/");
int last = path.lastIndexOf("/");
if (last >= 0) {
return path.substring(last + 1);
} else {
return path;
}
}
/**
* 字符串的編碼格式轉換
*
* @param value
* -- 要轉換的字符串
* @param oldCharset
* -- 原編碼格式
* @param newCharset
* -- 新編碼格式
* @return
*/
public static String convEncoding(byte[] value, String oldCharset,
String newCharset) {
OutputStreamWriter outWriter = null;
ByteArrayInputStream byteIns = null;
ByteArrayOutputStream byteOuts = new ByteArrayOutputStream();
InputStreamReader inReader = null;
char cbuf[] = new char[1024];
int retVal = 0;
try {
byteIns = new ByteArrayInputStream(value);
inReader = new InputStreamReader(byteIns, oldCharset);
outWriter = new OutputStreamWriter(byteOuts, newCharset);
while ((retVal = inReader.read(cbuf)) != -1) {
outWriter.write(cbuf, 0, retVal);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inReader != null)
inReader.close();
if (outWriter != null)
outWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String temp = null;
try {
temp = new String(byteOuts.toByteArray(), newCharset);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// System.out.println("temp" + temp);
return temp;
}
/**
* 顯示提示信息
*
* @param message
* -- 信息內容
* @param params
* -- 參數
*/
private static void info(String message, Object[]params) {
message = MessageFormat.format(message, params);
System.out.println(message);
}
}