1 packagecom.hy.util;2
3 import info.monitorenter.cpdetector.io.*;4
5 importjava.io.BufferedReader;6 importjava.io.File;7 importjava.io.FileInputStream;8 importjava.io.FileNotFoundException;9 importjava.io.FileOutputStream;10 importjava.io.IOException;11 importjava.io.InputStreamReader;12 importjava.io.OutputStreamWriter;13
14 public classCharacterChange{15
16 public static void main(String[] args) throwsFileNotFoundException, IOException {17
18 //封装目录,需要修改文件格式的路径
19 File srcFolder = new File("F:\\test");20
21 String newCharater = "GBK";22
23 getAllJavaFilePaths(srcFolder, newCharater);24 }25
26 private static void getAllJavaFilePaths(File srcFolder, String newCharater) throwsIOException {27
28 //获取该目录下所有的文件或者文件夹的File数组
29 File[] fileArray =srcFolder.listFiles();30
31 //遍历该File数组,得到每一个File对象
32 for(File file : fileArray) {33
34 //继续判断是否以特定文件结尾,不是的话继续调用getAllJavaFilePaths()方法
35 if(file.isDirectory()) {36 getAllJavaFilePaths(file, newCharater);37 } else{38 if (file.getName().endsWith(".sql")) {39 try{40 FileInputStream fis = newFileInputStream(file);41 //oldcCharacter 获取特定的字符集
42 String oldcCharacter =getChartsetName(file);43 InputStreamReader isr = newInputStreamReader(fis, oldcCharacter);44 BufferedReader br = newBufferedReader(isr);45 String str = null;46 //创建StringBuffer字符串缓存区
47 StringBuffer sb = newStringBuffer();48 //通过readLine()方法遍历读取文件
49 while ((str = br.readLine()) != null) {50 //使用readLine()方法无法进行换行,需要手动在原本输出的字符串后面加"\n"或"\r"
51 str += "\n";52 sb.append(str);53 }54 String fileSource =sb.toString();55 //以GBK格式写入文件,file.getAbsolutePath()即该文件的绝对路径,false代表不追加直接覆盖,true代表追加文件
56 FileOutputStream fos = new FileOutputStream(file.getAbsolutePath(), false);57 OutputStreamWriter osw = newOutputStreamWriter(fos, newCharater);58 try{59 osw.write(fileSource);60 System.out.println(61 "将:" + oldcCharacter + " 的文件:" + file.getAbsolutePath() + "修改字符集为:" +newCharater);62 } finally{63 osw.flush();64 osw.close();65 fos.close();66 br.close();67 isr.close();68 fis.close();69 }70 } catch(Exception e) {71 }72 } else{73 System.err.println("该文件以忽略:" +file.getAbsolutePath());74 }75 }76 }77 }78
79 public staticString getChartsetName(File file) {80 String chartsetName = null;81 //获取文件编码格式
82 CodepageDetectorProxy detector =CodepageDetectorProxy.getInstance();83 detector.add(new ParsingDetector(true));84 detector.add(JChardetFacade.getInstance());85 detector.add(ASCIIDetector.getInstance());86 detector.add(UnicodeDetector.getInstance());87 java.nio.charset.Charset charset = null;88 try{89 if (file != null) {90 charset =detector.detectCodepage(file.toURL());91 }92 } catch(Exception ex) {93 ex.printStackTrace();94 }95 if (charset != null) {96 chartsetName =charset.name();97 } else{98 chartsetName = "未知的编码";99 }100 returnchartsetName;101 }102 /*
103 * 104 net.sourceforge.jchardet105 jchardet106 1.0107 108 109 antlr110 antlr111 2.7.7112 113 */
114 }