在当前工作空间 新建一个java工程,修改项目路径, 然后点击此java 文件右键运行
注意: 运行前最好关闭 当前修改项目打开的文件,运行完毕 输出控制台 会显示 运行的路径, 你这个时候要先去刷新你的 修改的项目
package com.demo;
import java.io.BufferedReader;
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.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConverEncoding {
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
//项目空间所在路径 >项目名
String srcDir = "F:\\KingWorkSpace\\RippleDemo\\src";
List<String> files = new ArrayList<String>();
fetchFileList(srcDir, files, ".java");
for(String fileName : files){
convert(fileName, "GBK", fileName, "UTF-8");
}
}
public static void convert(String oldFile, String oldCharset, String newFlie, String newCharset){
BufferedReader bin;
FileOutputStream fos;
StringBuffer content = new StringBuffer();
try {
System.out.println(oldFile);
bin = new BufferedReader(new InputStreamReader(new FileInputStream(oldFile),"gbk"));
String line = null;
while((line=bin.readLine())!=null){
// System.out.println("content:" + content);
content.append(line);
content.append(System.getProperty("line.separator"));
}
bin.close();
File dir = new File(newFlie.substring(0, newFlie.lastIndexOf("\\")));
if(!dir.exists()){
dir.mkdirs();
}
fos = new FileOutputStream(newFlie);
Writer out = new OutputStreamWriter(fos, newCharset);
out.write(content.toString());
out.close();
fos.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void fetchFileList(String strPath, List<String> filelist, final String regex) {
File dir = new File(strPath);
File[] files = dir.listFiles();
Pattern p = Pattern.compile(regex);
if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fetchFileList(files[i].getAbsolutePath(), filelist, regex);
} else {
String strFileName = files[i].getAbsolutePath().toLowerCase();
Matcher m = p.matcher(strFileName);
if(m.find()){
filelist.add(strFileName);
}
}
}
}
}