package com.xiangshauii;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
public class GBKToUTF8 {
//要轉換的目錄--目標資源URL
public final static String targetFileUrl="F:/gq";
//遍歷目錄,將文件從GBK轉換成UTF-8
public static void fileList(File file) {
File rootFile = file;
File[] files = rootFile.listFiles();
if (files != null) {
for (File f:files) {
if (!f.isDirectory()) {
codeConvert(f);
}
System.out.println(f.getPath());
fileList(f);//遞歸調用子文件夾下的文件
}
}
}
public static void main(String[] args){
File file = new File(targetFileUrl);
GBKToUTF8.fileList(file);
}
public static void codeConvert(File file) {
try{
BufferedReader br = new BufferedReader(new InputStreamReader
(new FileInputStream(file), Charset.forName("GBK")));
StringBuilder sb = new StringBuilder();
String str;
while ((str = br.readLine())!= null) {
sb.append(str);
sb.append("\n");
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),Charset.forName("UTF-8")));
bw.write(sb.toString());
bw.flush();
bw.close();
//br.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
————————————————
版權聲明:本文為CSDN博主「香帥xs」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xiangshuai198807/article/details/79459915
