Java工程編碼格式由GBK轉(zhuǎn)化成utf-8的具體實(shí)現(xiàn)
在寫項(xiàng)目的過程中我發(fā)現(xiàn)有的地方編碼格式被設(shè)置成了
gbk如果用eclipse等工具直接改回utf-8編碼格式則會(huì)出現(xiàn)亂碼。
在這里搞了一個(gè)工具,直接輸入之前的編碼格式跟要改的編碼格式就會(huì)自動(dòng)轉(zhuǎn)換

轉(zhuǎn)換完成后直接設(shè)置為更改后的格式即可

以下是源代碼:
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.util.Scanner;
/**
* 把gbk編碼的程序變換為用utf-8的格式編碼
*
* 此程序只是為了改變 .java文件的編碼格式如果你想要變換為其他格式只需要改變下面對(duì)應(yīng)的編碼按格式
*
* @author 明金同學(xué) CSDN:https://ymjin.blog.csdn.net/
*/
public class Files {
/**
*
* @param args
* @throws UnsupportedEncodingException
* @throws IOException
*/
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
Scanner scan = new Scanner(System.in);
System.out.println("請(qǐng)輸入需要改變編碼格式的文件位置");
String str = scan.nextLine();
File file = new File(str);
System.out.println("文件的初始編碼");
String bm1 = scan.nextLine();
System.out.println("文件需要轉(zhuǎn)換成的編碼");
String bm2 = scan.nextLine();
getAllFiles(file, bm1, bm2);
}
/**
*
* @param file 要編譯的文件
* @param bm1 文件的初始編碼
* @param bm2 文件需要轉(zhuǎn)換成的編碼
* @throws FileNotFoundException 文件找不到
* @throws UnsupportedEncodingException 編碼出錯(cuò)
* @throws IOException io異常
*/
public static void getAllFiles(File file, String bm1, String bm2) throws FileNotFoundException, UnsupportedEncodingException, IOException {
if (file.isDirectory()) {
File[] test = file.listFiles();
for (File test1 : test) {
//類的名字
String str = test1.getPath();
if (str.endsWith("java") & test1.isFile()) {
String[] s = str.split("\\.");
String filecope = s[0] + "cope." + s[1];
System.out.println(filecope);
File fil = new File(filecope);
//轉(zhuǎn)格式
InputStreamReader isr = new InputStreamReader(new FileInputStream(test1), bm1);
OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(fil), bm2);
int re = -1;
while ((re = isr.read()) != -1) {
osr.write(re);
}
isr.close();
osr.close();
InputStreamReader isrr = new InputStreamReader(new FileInputStream(fil), bm2);
OutputStreamWriter osrw = new OutputStreamWriter(new FileOutputStream(test1), bm2);
int r = -1;
while ((r = isrr.read()) != -1) {
osrw.write(r);
}
isrr.close();
osrw.close();
boolean d = fil.delete();
System.out.println(str + "文件轉(zhuǎn)換utf-8成功:" + d);
}
getAllFiles(test1, bm1, bm2);
}
}
}
}
到此這篇關(guān)于Java工程編碼格式由GBK轉(zhuǎn)化成utf-8的具體實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Java編碼格式GBK轉(zhuǎn)utf-8內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用IDEA進(jìn)行安卓開發(fā)的詳細(xì)圖文教程
安卓開發(fā)本身就是Java開發(fā)的一個(gè)分支,我們要確保計(jì)算機(jī)已經(jīng)安裝好JDK并做好了相關(guān)的配置,下面這篇文章主要給大家介紹了關(guān)于如何使用IDEA進(jìn)行安卓開發(fā)的詳細(xì)圖文教程,需要的朋友可以參考下2023-04-04
IntelliJ IDEA中打開拼寫檢查與忽略提示曲線的方法
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中打開拼寫檢查與忽略提示曲線的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Java的ConcurrentHashMap中不能存儲(chǔ)null的原因解析
眾所周知,在Java中Map可以存儲(chǔ)null,而ConcurrentHashMap不能存儲(chǔ)null值,那么為什么呢?今天通過源碼分析給大家詳細(xì)解讀,感興趣的朋友一起看看吧2022-07-07
Springboot+redis+Interceptor+自定義annotation實(shí)現(xiàn)接口自動(dòng)冪等
本篇文章給大家介紹了使用springboot和攔截器、redis來優(yōu)雅的實(shí)現(xiàn)接口冪等,對(duì)于冪等在實(shí)際的開發(fā)過程中是十分重要的,因?yàn)橐粋€(gè)接口可能會(huì)被無數(shù)的客戶端調(diào)用,如何保證其不影響后臺(tái)的業(yè)務(wù)處理,如何保證其只影響數(shù)據(jù)一次是非常重要的,感興趣的朋友跟隨小編一起看看吧2019-07-07

