Java实现将GBK编码格式的文件夹中所有文件都转化为UTF-8格式的文件,编码格式转化

时间:2023-01-10 20:48:58

package CodeConvert;


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 {

//遍历文件

publicstatic void fileList(File file) {

File rootFile =file;

File[] files =rootFile.listFiles();

if (files !=null) {

for (Filef:files) {

if (!f.isDirectory()) {

codeConvert(f);

}

System.out.println(f.getPath());

fileList(f);//递归调用子文件夹下的文件

}

}

}

//另一种展示方式,自己玩的与实现功能无关

publicstatic void fileList1(File file,intnode){

node++;

File rootFile =file;

File[] files =rootFile.listFiles();

if (files !=null) {

for(Filef:files){

for (inti=0; i < node; i++) {

if (i ==node-1) {

System.out.print("|-");

}else System.out.print(" ");

}

System.out.println(f.getName());

fileList1(f,node);

}

}

}


publicstatic void main(String[] args){

File file =new File("/Users/...../GBK");

GBKtoUTF8.fileList(file);

//GBKtoUTF8.codeConvert(file);

}

publicstatic 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 (Exceptione){

e.printStackTrace();

}

}

}