首頁  >  文章  >  Java  >  如何利用Java將GBK工程轉為uft8的方法分享

如何利用Java將GBK工程轉為uft8的方法分享

黄舟
黄舟原創
2017-08-13 09:34:001563瀏覽

本篇文章主要介紹了用java將GBK工程轉為uft8的方法實例,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧

本文介紹了用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();
  }
}

以上是如何利用Java將GBK工程轉為uft8的方法分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn