Java批量转换文件编码

来源:互联网 发布:求解无约束优化问题 编辑:程序博客网 时间:2024/05/17 08:52
 

转载:http://hi.baidu.com/zhjunxue/blog/item/5f94ca8af9da00d4fc1f105f.html

package file;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
* 可用来批量转换代码的编码.并保持文件段落及缩进
* @author Justin
*
*/
public class FileList {
//获取某个目录下面的所有文件
public File[] getFileDir(String dir) {
   File dataDir = new File(dir);
   File[] dataFiles = dataDir.listFiles();
   return dataFiles;
}

public boolean getFileList(String dir) {
   boolean isSucc = false;
   File dataDir = new File(dir);
   File[] dataFiles = dataDir.listFiles();
   try {
    for (int i = 0; i < dataFiles.length; i++) {
     //如果是目录则递归调用此方法.继续检索
     if (dataFiles[i].isDirectory()) {
      getFileList(dataFiles[i].getCanonicalPath());
     } else {
      //文件过滤,如无需此项注释此处即可
      if (dataFiles[i].isFile()
        && (dataFiles[i].getName().endsWith(".jsp")
          || dataFiles[i].getName().endsWith(".html") || dataFiles[i]
          .getName().endsWith(".htm"))
        || dataFiles[i].getName().endsWith(".js")
        || dataFiles[i].getName().endsWith(".java")) {

       writeFile(dataFiles[i].getAbsolutePath(),
         dataFiles[i].getAbsolutePath()
           .substring(
             2,
             dataFiles[i].getAbsolutePath()
               .length()));
      }
     }

    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   return isSucc;
}
//以默认编码读取文件内容.并以字条串返回
private String readFile(String templet) {
   String templetContent = "";

   try {
    FileInputStream fileinputstream = new FileInputStream(templet);
    int length = fileinputstream.available();
    byte bytes[] = new byte[length];
    fileinputstream.read(bytes);
    fileinputstream.close();
    templetContent = new String(bytes);
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return templetContent;
}
// 以UTF-8读取文件内容.并以字条串返回
private String readFileUTF_8(String templet) {
   String templetContent = "";

   try {
    FileInputStream fileinputstream = new FileInputStream(templet);
    int length = fileinputstream.available();
    byte bytes[] = new byte[length];
    fileinputstream.read(bytes);
    fileinputstream.close();
    templetContent = new String(bytes, "UTF-8");
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return templetContent;
}
//在新文件夹建立相同的目录结构
private void makeDir(String path) {
   File fileStr = new File(path);
   File file = new File(fileStr.getParent());

   if (!file.exists()) {
    file.mkdirs();
   }
}

public boolean writeFile(String realPath, String fileName) {
   boolean isSucc = false;
   String templet = realPath;
   String newFile = "D:/NewFiles/"
     + fileName.substring(1, fileName.length());
   makeDir(newFile);
   String templateContent = "";
  
   //读取文件内容.如需以UTF-8读取.换成readFileUTF_8(filePath)即可
   templateContent = readFile(templet);
  
   //如果是网页或其它注明了编码的文件.用此方法替换
   templateContent = templateContent.replaceAll("gb2312", "UTF-8");
   templateContent = templateContent.replaceAll("gbk", "UTF-8");
  
   /*
   templateContent = templateContent.replaceAll("UTF-8", "GBK");
   templateContent = templateContent.replaceAll("utf-8", "GBK");*/
   //templateContent = templateContent.replaceAll("GB2312", "UTF-8");
   //templateContent = templateContent.replaceAll("gbk", "UTF-8");
   //System.out.println("输出:" + templateContent);
  
   isSucc = writeNewFile(newFile, templateContent);
   System.out.println("输出:" + newFile);

   return isSucc;

}
/**
* 以UTF-8形式写新文件
* @param newFile
* @param templetContent
* @return
*/
private boolean writeNewFile(String newFile, String templetContent) {
   boolean isSucc = false;
   try {
    FileOutputStream fout = new FileOutputStream(newFile);
    OutputStreamWriter out = new OutputStreamWriter(
      new BufferedOutputStream(fout), "UTF-8");
    out.write(templetContent);
    out.close();
    fout.close();
    isSucc = true;
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
   return isSucc;
}

/**
* @param args
*/
public static void main(String[] args) {

   FileList fl = new FileList();
   fl.getFileList("D:\\speciality");

}

}


原创粉丝点击