JAVA源文件GBK编码批量转为UTF-8(暂没找到IDE直接转换方法)

背景

       今天使用eclipse打开GBK项目时,发现显示中文乱码,本来不想处理的,但是后面编写的文件都被以UTF-8默认编码保存,由于eclipse设置的工程默认编码时UTF-8,导致整个文件的格式变成了中文乱码,并且无法再还原了。后面在网上搜了一下,发现在方面的资料偏少,不过最终还是搜到了一个文章,就是自己写代码把文件编码批量修改为UTF-8。

原理

      就是我们使用GBK编码读取文件,然后,再以UTF-8编码写入文件并覆盖。不过还是有问题,因为之前由于使用UTF-8保存的文件编码还是不能还原,最后只能手动还原。

文件操作工具类编写

读取文件编码

/**
     * 读取文件内容
     * 
     * @param file
     *            目标文件对象
     * @param encode
     *            读取文件时使用的编码
     * @return
     * @throws Exception
     */
public static String readFile(File file, String encode) throws Exception {
 
        FileInputStream fis = null;
        StringBuilder content = new StringBuilder();
        InputStreamReader ir = null;
 
        try {
            fis = new FileInputStream(file);
            ir = new InputStreamReader(fis, encode);
            char[] tempbytes = new char[1024];
            int byteread = 0;
            while ((byteread = ir.read(tempbytes)) != -1) {
                content.append(new String(tempbytes, 0, byteread));
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        } 
        finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ir != null) {
                ir.close();
            }
        }
 
        return content.toString();
    }

写入文件

/**
     * 创建文件并写入内容
     * 
     * @param path
     *            文件路径,不包括文件名
     * @param fileName
     *            文件名,带后缀
     * @param content
     *            内容
     * @param encode
     *            写入时的编码
     * @return
     * @throws Exception
     */
    public static String write(String path, String fileName, String content, String encode) throws Exception {      
        
        if (isNull(path)) {
            System.out.println("Path is null");
            return "error";
        }
        
        Writer writer = null;
        
        try {
            
            File files = new File(path);
            if (!files.exists()) {
                files.mkdirs();
            }
            
            path += fileName;
            File file = new File(path);
            System.out.println(file.getPath());
            if (file.exists()) file.delete();
            file.createNewFile();
            
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encode));  
            writer.write(content);
        }
        finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
        
        return "success";
    }

启动类

public static void main(String[] args) throws Exception {
        
        String path = "D:\\iflyzunhong\\workspace\\httpserver\\src";
        readFiles(path);
        System.out.println("file count = " + fileCount);
        
    }
 
    static void readFiles(String path) throws Exception {
 
        File file = new File(path);
 
        File[] fileAry = file.listFiles();
 
        for (int i = 0; i < fileAry.length; i++) {
 
            File f = fileAry[i];
 
            if (f.isFile()) {
 
                if (!f.getName().contains(".")) {
                    continue;
                }
                
                String suffix = f.getName().substring(f.getName().lastIndexOf("."));
                
                if (suffix.equals(".java")) {
                    FileUtil.write(f.getParent() + "/", f.getName(), FileUtil.readFile(f, "GBK"), "UTF-8");
                    System.out.println(f.getPath());
                    fileCount++;
                }
                
            } 
            else if (f.isDirectory()) {
                readFiles(f.getPath());
            }
 
        }
 
    }

在这里比较重点的是,FileUtil.write(f.getParent() + "/", f.getName(), FileUtil.readFile(f, "GBK"), "UTF-8")这一行,

这里是先将文件以文件以GBK编码格式读取,然后再以UTF-8格式写入覆盖。

我们执行一下这个程序就可以批量转换编码格式了。

总结

       在网上搜了很久,没有搜到使用工具直接批量修改编码格式的方法,不过这个方法也还能满足要求。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值