java批量将utf-8转化为gbk

发布时间:2014-10-23 23:30:50
来源:分享查询网

import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Writer; public class UTFToGBK { /** * @param args */ private static void transferFile(String srcFileName, String destFileName) throws IOException { File OldFile = new File(srcFileName); if (!OldFile.exists()) { System.out.print("要转化的文件路径不存在!"); return; } File NewFile = new File(destFileName); // NewFile.mkdir(); // 如果是文件,则转换 if (OldFile.isFile()) { String line_separator = System.getProperty("line.separator"); FileInputStream fis = new FileInputStream(srcFileName); StringBuffer content = new StringBuffer(); DataInputStream in = new DataInputStream(fis); BufferedReader d = new BufferedReader(new InputStreamReader(in, "utf-8")); String line = null; while ((line = d.readLine()) != null) content.append(line + line_separator); d.close(); in.close(); fis.close(); // 转化之后写入新文件 Writer ow = new OutputStreamWriter(new FileOutputStream( destFileName), "GBK"); ow.write(content.toString()); ow.close(); } else { if (!NewFile.exists()) { NewFile.mkdir(); } // 查找目录下面的所有文件与文件夹 File[] childFiles = OldFile.listFiles(); for (int i = 0, n = childFiles.length; i < n; i++) { File child = childFiles[i]; String childFrom = srcFileName + "/" + child.getName(); String childTo = destFileName + "/" + child.getName(); transferFile(childFrom, childTo); } } } public static void main(String[] args) { // TODO Auto-generated method stub try { transferFile("C:\\Users\\Dove\\Desktop\\java\\tpartnerregbaseinfo", "C:\\Users\\Dove\\Desktop\\tpartnerregbaseinfo"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

返回顶部
查看电脑版