欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

用java将GBK工程转为uft8的方法实例

时间:2021-05-13 08:12:49|栏目:JAVA代码|点击:

本文介绍了用java将GBK工程转为uft8,分享给大家,具体如下:

windows下的默认编码为GBK还有gb2312,如何把gbk的java工程转为utf8的呢,如果直接修改工程编码,其实里面的java文件中中文是会乱码的,写了个批量转换java工程的程序,消遣一下。

为什么要转码?

有些老的项目,或者朋友的项目之前没注意在windows上不是utf8,而你有需要看注释或者什么,总不能一个文件一个文件的去改编码属性吧。

本程序试用范围

gbk的代码,或者gb2312的工程均可以转换

编码转换的思路

本来想做成一个通用的会自动检测编码,自动转换的程序。但是由于判断编码类型不准,所以做成了针对GBK的转换。

  1. 制定gbk编码把文件流读进来,加载到内存,转为String类型的内容
  2. 将String内容转为utf8的String
  3. 将String内容写入文件

核心代码:

public class TransferProject{
  public static void transferFile(String pathName,intdepth)throwsException{
    File dirFile = new File(pathName);
    if (!isValidFile(dirFile)) return;
    //获取此目录下的所有文件名与目录名
    String[] fileList = dirFile.list();
    int currentDepth = depth + 1;
    for (int i = 0; i < fileList.length; i++) {
      String string = fileList[i];
      File file = new File(dirFile.getPath(), string);
      String name = file.getName();
      //如果是一个目录,搜索深度depth++,输出目录名后,进行递归
      if (file.isDirectory()) {
        //递归
        transferFile(file.getCanonicalPath(), currentDepth);
      } else {
        if (name.contains(".java") || name.contains(".properties") || name.contains(".xml")) {
          readAndWrite(file);
          System.out.println(name + " has converted to utf8 ");
        }
      }
    }
  }

 
  private static boolean isValidFile(File dirFile)throwsIOException{
    if (dirFile.exists()) {
      System.out.println("file exist");
      return true;
    }
    if (dirFile.isDirectory()) {
      if (dirFile.isFile()) {
        System.out.println(dirFile.getCanonicalFile());
      }
      return true;
    }
    return false;
  }

  private static void readAndWrite(File file)throwsException{
    String content = FileUtils.readFileByEncode(file.getPath(), "GBK");
    FileUtils.writeByBufferedReader(file.getPath(), new String(content.getBytes("UTF-8"), "UTF-8"));
  }

  public static void main(String[] args)throwsException{
    //程序入口,制定src的path
    String path = "/Users/mac/Downloads/unit06_jdbc/src";
    transferFile(path, 1);
  }
}
public class FileUtils{
  public static void writeByBufferedReader(String path, String content){
    try {
      File file = new File(path);
      file.delete();
      if (!file.exists()) {
        file.createNewFile();
      }

      FileWriter fw = new FileWriter(file, false);
      BufferedWriter bw = new BufferedWriter(fw);
      bw.write(content);
      bw.flush();
      bw.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  public staticStringreadFileByEncode(String path, String chatSet)throwsException{
    InputStream input = new FileInputStream(path);
    InputStreamReader in = new InputStreamReader(input, chatSet);
    BufferedReader reader = new BufferedReader(in);
    StringBuffer sb = new StringBuffer();
    String line = reader.readLine();
    while (line != null) {
      sb.append(line);
      sb.append("\r\n");
      line = reader.readLine();
    }
    return sb.toString();
  }
}

上一篇:通过实例学习Spring @Required注释原理

栏    目:JAVA代码

下一篇:浅谈Spring @Async异步线程池用法总结

本文标题:用java将GBK工程转为uft8的方法实例

本文地址:http://www.codeinn.net/misctech/120446.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有