批量把文件编码由GBK转UTF-8

这是一个Java程序,用于批量将指定目录下的GBK编码文件转换为UTF-8编码。程序通过递归遍历目录,读取GBK编码的文件内容,然后将其转换为UTF-8并保存到新的目录中。
摘要由CSDN通过智能技术生成
Java代码   收藏代码
  1. import java.io.ByteArrayInputStream;  
  2. import java.io.ByteArrayOutputStream;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9. import java.io.OutputStreamWriter;  
  10. import java.io.UnsupportedEncodingException;  
  11. import java.text.MessageFormat;  
  12.   
  13. /** 
  14.  * 批量把文件编码由GBK转为UTF8,可以继续完善做成在命令行中执行的程序, 可以添加文件名过滤等功能,暂时未实现。 
  15.  *  
  16.  */  
  17. public class FileGBK2UTF8 {  
  18.   
  19.     public static void main(String[] args) {  
  20.         // 需要转换的文件目录  
  21.         String fromPath = "D:\\input";  
  22.         // 转换到指定的文件目录  
  23.         String toPath = "D:\\output";  
  24.   
  25.         info("start transform [from path]={0} [to path]={1}", fromPath, toPath);  
  26.   
  27.         // 递归取到所有的文件进行转换  
  28.         transform(fromPath, toPath);  
  29.     }  
  30.   
  31.     /** 
  32.      * 把一个目录中的文件转换到另一个目录中 
  33.      *  
  34.      * @param fromPath 
  35.      *            -- 来源文件目录 
  36.      * @param toPath 
  37.      *            -- 目标文件目录 
  38.      * @return 
  39.      */  
  40.     public static boolean transform(String fromPath, String toPath) {  
  41.         File ftmp = new File(fromPath);  
  42.         if (!ftmp.exists()) {  
  43.             info("转换文件路径错误!");  
  44.             return false;  
  45.         }  
  46.   
  47.         info("frompath is [{0}], topath is [{1}]", fromPath, toPath);  
  48.   
  49.         // 如果是文件,则转换,结束  
  50.         if (ftmp.isFile()) {  
  51.             byte[] value = fileToBytes(fromPath);  
  52.             String content = convEncoding(value, "gbk""utf-8");  
  53.             return saveFileUtf8(toPat
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值