Java工程編碼格式由GBK轉(zhuǎn)化成utf-8的具體實現(xiàn)
在寫項目的過程中我發(fā)現(xiàn)有的地方編碼格式被設(shè)置成了
gbk
如果用eclipse等工具直接改回utf-8
編碼格式則會出現(xiàn)亂碼。
在這里搞了一個工具,直接輸入之前的編碼格式跟要改的編碼格式就會自動轉(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文件的編碼格式如果你想要變換為其他格式只需要改變下面對應(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("請輸入需要改變編碼格式的文件位置"); 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 編碼出錯 * @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的具體實現(xiàn)的文章就介紹到這了,更多相關(guān)Java編碼格式GBK轉(zhuǎn)utf-8內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于SpringCloud的微服務(wù)以及組件詳解
這篇文章主要介紹了關(guān)于SpringCloud的微服務(wù)以及組件詳解,是一個更高層次的、 架構(gòu)視角的綜合性大型項目, 他的目標(biāo)是構(gòu)建一套標(biāo)準(zhǔn)化的微服務(wù)解決方案,需要的朋友可以參考下2023-05-05詳解Java中Array和ArrayList的比較和轉(zhuǎn)換
在 Java 編程中,arrays 和 arraylists 都是基本的數(shù)據(jù)結(jié)構(gòu),用來存放數(shù)據(jù)集合,雖然兩者的用途一樣,但是它們的特點極大地影響應(yīng)用的性能和靈活性,本文探討 arrays 和 arraylists 的重要特性,它們各自的強(qiáng)項和弱點,,需要的朋友可以參考下2023-08-08通過Maven進(jìn)行jedis連接redis的實現(xiàn)
這篇文章主要介紹了通過Maven進(jìn)行jedis連接redis的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Spring Boot整合SSE實時通信的問題小結(jié)
本文介紹了服務(wù)器發(fā)送事件(Server-Sent Events,SSE)技術(shù),其主要特點包括單向數(shù)據(jù)流、自動重連、自定義事件類型等,SSE適用于實時更新場景,如新聞推送、評論系統(tǒng)等,感興趣的朋友跟隨小編一起看看吧2025-01-01java使用stream判斷兩個list元素的屬性并輸出方式
這篇文章主要介紹了java使用stream判斷兩個list元素的屬性并輸出方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06