這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)利用java如何將GBK轉(zhuǎn)換為uft8,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

具體如下:
windows下的默認(rèn)編碼為GBK還有g(shù)b2312,如何把gbk的java工程轉(zhuǎn)為utf8的呢,如果直接修改工程編碼,其實(shí)里面的java文件中中文是會(huì)亂碼的,寫了個(gè)批量轉(zhuǎn)換java工程的程序,消遣一下。
為什么要轉(zhuǎn)碼?
有些老的項(xiàng)目,或者朋友的項(xiàng)目之前沒注意在windows上不是utf8,而你有需要看注釋或者什么,總不能一個(gè)文件一個(gè)文件的去改編碼屬性吧。
本程序試用范圍
gbk的代碼,或者gb2312的工程均可以轉(zhuǎn)換
編碼轉(zhuǎn)換的思路
本來想做成一個(gè)通用的會(huì)自動(dòng)檢測編碼,自動(dòng)轉(zhuǎn)換的程序。但是由于判斷編碼類型不準(zhǔn),所以做成了針對GBK的轉(zhuǎn)換。
制定gbk編碼把文件流讀進(jìn)來,加載到內(nèi)存,轉(zhuǎn)為String類型的內(nèi)容
將String內(nèi)容轉(zhuǎn)為utf8的String
將String內(nèi)容寫入文件
核心代碼:
public class TransferProject{
public static void transferFile(String pathName,intdepth)throwsException{
File dirFile = new File(pathName);
if (!isValidFile(dirFile)) return;
//獲取此目錄下的所有文件名與目錄名
String[] fileList = dirFile.list();
int currentDepth = depth + 1;
for (int i = 0; i < fileList.length; i++) {
String string = fileList[i];
File file = new File(dirFile.getPath(), string);
String name = file.getName();
//如果是一個(gè)目錄,搜索深度depth++,輸出目錄名后,進(jìn)行遞歸
if (file.isDirectory()) {
//遞歸
transferFile(file.getCanonicalPath(), currentDepth);
} else {
if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) {
readAndWrite(file);
System.out.println(name + " has converted to utf8 ");
}
}
}
}
private static boolean isValidFile(File dirFile)throwsIOException{
if (dirFile.exists()) {
System.out.println("file exist");
return true;
}
if (dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
return true;
}
return false;
}
private static void readAndWrite(File file)throwsException{
String content = FileUtils.readFileByEncode(file.getPath(), "GBK");
FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8"));
}
public static void main(String[] args)throwsException{
//程序入口,制定src的path
String path = "/Users/mac/Downloads/unit06_jdbc/src";
transferFile(path, 1);
}
}public class FileUtils{
public static void writeByBufferedReader(String path, String content){
try {
File file = new File(path);
file.delete();
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public staticStringreadFileByEncode(String path, String chatSet)throwsException{
InputStream input = new FileInputStream(path);
InputStreamReader in = new InputStreamReader(input, chatSet);
BufferedReader reader = new BufferedReader(in);
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
while (line != null) {
sb.append(line);
sb.append("\r\n");
line = reader.readLine();
}
return sb.toString();
}
}上述就是小編為大家分享的利用java如何將GBK轉(zhuǎn)換為uft8了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:利用java如何將GBK轉(zhuǎn)換為uft8-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.chinadenli.net/article28/iiecp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、網(wǎng)站改版、搜索引擎優(yōu)化、定制網(wǎng)站、定制開發(fā)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容