批量修改文件的编码方式

        前两天抓了一个网站到本地,但是这个网站的文件是繁体的,big5码,由于我现在使用日文系统,所以每次打开都要转换一下编码方式,觉得麻烦,因此写了这么一个类。
作用:1、修改文件的编码方式,比如原先的编码是big5,我把它转换成UTF-8。
      2、拷贝图片,读取图片然后把文件放到指定的目录。(这个地方可以扩展,我是用一个图片缩放类,修改过来的,因此这个部分可以修改为具有图片缩放功能的模块)。
      3、网页编码方式替换,我读取文件的时候直接把网页指定的编码也给替换成UTF-8了。

-----------------------------------------------------code start -----------------------------------------------------

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**
 * Change file's encoding
 * @author webkkk
 * @link www.webkkk.net
 *
 */
public class ConverEncoding {

 static String strDealObj = "C://temp//javascript";

 static String strNewPath = "C://temp//javascriptNew";

 static String[] strCanDoExtName = { "jpg", "jpeg", "gif" ,"png"};

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

  File objFile = new File(strDealObj);
  doDealWithObj(objFile, strNewPath);
 }

 private static void doDealWithObj(File objFile, String strNewPath) throws Exception {
  if (objFile.isDirectory()) {
   File[] objFiles = objFile.listFiles();
   for (int i = 0; i < objFiles.length; i++) {
    if (objFiles[i].isDirectory()) {
     doDealWithObj(objFiles[i], createPathName(strNewPath, objFiles[i].getName()));
    } else {
     // System.out.println(objFiles[i].getPath());
     // System.out.println(strNewPath);
     // System.out.println("-------------------");
     judgeFile(objFiles[i], strNewPath, "big5", "UTF-8");
    }
   }
  } else {
   // System.out.println(objFile.getPath());
   // System.out.println(strNewPath);
   // System.out.println("================");
   judgeFile(objFile, strNewPath, "big5", "UTF-8");
  }
 }

 private static String createPathName(String strPathName, String strFolderName) {
  if (strPathName.lastIndexOf("//") == strPathName.length()) {
   return strPathName + strFolderName;
  } else {
   return strPathName + "//" + strFolderName;
  }
 }

 private static void judgeFile(File oldFile, String newFilePath, String strOldEncoding,
   String strNewEncoding) throws Exception {
  File fileNewPath = new File(newFilePath);
  if (!fileNewPath.exists()) {
   fileNewPath.mkdirs();
  }
  String strFileName = oldFile.getName();
  String strExt = getFileExtension(strFileName);
  int intExt = isBelongPic(strExt, strCanDoExtName);
  if (intExt != -1) {
   copyPic(oldFile, newFilePath, intExt);
  } else {
   copyFile(oldFile, newFilePath, "big5", "UTF-8");
  }
 }

 private static void copyFile(File oldFile, String newFilePath, String strOldEncoding,
   String strNewEncoding) throws Exception {
  FileInputStream fileInputStream = null;
  InputStreamReader inputStreamRead = null;
  BufferedReader bufferRead = null;

  BufferedWriter newFileBW = null;
  OutputStreamWriter outputStreamWriter = null;
  FileOutputStream fileOutputStream = null;

  // boolean blnCopyOK = false;

  try {
   fileInputStream = new FileInputStream(oldFile);
   inputStreamRead = new InputStreamReader(fileInputStream, strOldEncoding);
   bufferRead = new BufferedReader(inputStreamRead);

   File copyFile = new File(createPathName(newFilePath, oldFile.getName()));
   fileOutputStream = new FileOutputStream(copyFile, false);
   outputStreamWriter = new OutputStreamWriter(fileOutputStream, strNewEncoding);
   newFileBW = new BufferedWriter(outputStreamWriter);

   String strTSVLine = "";
   while ((strTSVLine = bufferRead.readLine()) != null) {
    if (strTSVLine.equals("")) {
     continue;
    }
    newFileBW.write(strTSVLine.replaceAll(" charset=big5", " charset=UTF-8") + "/r/n");
   }
   // blnCopyOK = true;
  } finally {
   if (bufferRead != null)
    bufferRead.close();

   // if (blnCopyOK) {
   // oldFile.delete();
   // }
   if (newFileBW != null) {
    newFileBW.flush();
    newFileBW.close();
   }
  }
 }

 private static void copyPic(File oldFile, String newFilePath, int intPicType) throws Exception {
  String strFileName;

  strFileName = oldFile.getName(); // 獲得文件名

  // 獲得文件擴展名
  String strExtType = getFileExtension(strFileName);

  // *圖片文件處理
//  File imagefile = new File(strFileName); // 讀入文件

  Image src_image = javax.imageio.ImageIO.read(oldFile);

  // 獲得圖片文件的高度和寬度
  int width = src_image.getWidth(null); // 得到源圖寬

  int height = src_image.getHeight(null);

  // 在申請的圖片緩存區中重繪圖片
  BufferedImage buf_image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);

  // 繪制縮小后的圖
  buf_image.getGraphics().drawImage(src_image, 0, 0, width, height, null);

  // 重新建立文件輸出流,并編碼
  FileOutputStream out = new FileOutputStream(createPathName(newFilePath, strFileName));

  // 輸出到文件流

  JPEGImageEncoder jpg_encoder = JPEGCodec.createJPEGEncoder(out);

  jpg_encoder.encode(buf_image);

  // 使用JPEG編碼文件

  // 關閉文件流
  out.close();
 }

 public static String getFileExtension(String fileName) {
  return fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
 }

 private static int isBelongPic(String strFileExt, String[] strCanDoPic) {
  for (int i = 0; i < strCanDoPic.length; i++) {
   if (strFileExt.equalsIgnoreCase(strCanDoPic[i])) {
    return i;
   }
  }
  return -1;
 }
}

-----------------------------------------------------code end-----------------------------------------------------

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值