/*
*/package com.blog.code.encoding;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader;/**
* TODO: 文件注释 * <p> * <p> * Version: V2.0 * <p> * 作者: ljj * <p> * 日期: 2016年9月19日 * <p> * 负责人: ljj * <p> * <p> */ public class ConvertEncoding { /** * * srcEncode 源编码格式 * targetEncode 目标编码格式 * srcPath 源路径 * targetPath 目标路径 * relativePath 相对于源路径的路径 * @param filter 文件类型过滤器 */ public void convertEncoding(String srcEncode, String targetEncode, String srcPath, String targetPath, String relativePath, FileFilter filter) { File srcFile = new File(srcPath); if (srcFile.isFile()) { try { File targetFile = new File(targetPath+ File.separatorChar + srcFile.getName()); if (!targetFile.exists()) { targetFile.createNewFile(); } BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(srcFile), "GBK")); BufferedWriter writer = new BufferedWriter(new FileWriter(targetFile)); String line; while ((line = reader.readLine()) != null) { writer.write(new String(line.getBytes(), targetEncode)); writer.newLine(); } System.out.println(targetFile.getAbsolutePath()); reader.close(); writer.flush(); writer.close(); } catch(Exception e) { e.printStackTrace(); } } else if (srcFile.isDirectory()) { File[] files = srcFile.listFiles(filter); // 建立目标目录 File targetFileDir = new File(targetPath + relativePath); if (!targetFileDir.exists()) { targetFileDir.mkdirs(); } for (File f : files) { convertEncoding(srcEncode, targetEncode, f.getAbsolutePath(), targetFileDir.getAbsolutePath(), f.getAbsolutePath().substring(srcPath.length()), filter); } } }public static void main(String[] args)
{ ConvertEncoding ce = new ConvertEncoding(); ce.convertEncoding("GBK", "UTF-8", "E:/office_engine/src", "E:/office_engine_utf8/src", "", new FileFilter() { public boolean accept(File pathname) { return pathname.isDirectory() || pathname.getName().endsWith("java"); } }); }}