liuyuqi-dellpc 7 years ago
commit
0b3d563b0d
4 changed files with 120 additions and 0 deletions
  1. 14 0
      README.md
  2. 29 0
      java/convert.java
  3. 38 0
      php/convert.php
  4. 39 0
      python/convert.py

+ 14 - 0
README.md

@@ -0,0 +1,14 @@
+# gbk2utf8
+## 功能介绍
+之前开发的很多eclipse项目源码设置的是ANSI,即随系统编码,默认GBK编码。那么现在开发的项目源码都是UTF-8编码。很多工具打开都会出现乱码的情况。文件众多,于是开发这个项目,实现gbk文件批量转换为utf-8编码。
+
+在网上找了一些资料,开发语言很多,这里分别用到的三种语言:Python、java、php来实现。
+
+## 使用
+1. 配置项目文件夹
+python:pip install  os, chardet, codecs
+
+2. java执行 convert.java;php执行php convert.php; python执行python convert.py即可。
+
+## 进度
+2017-12-17 python部分完成,对项目所有非utf-8编码的java文件全部转为utf-8编码。

+ 29 - 0
java/convert.java

@@ -0,0 +1,29 @@
+package cwj.bbb;
+
+import java.io.*;
+import java.util.Collection;
+
+import org.apache.commons.io.FileUtils;
+/*
+ * 批量把文本文档的GBK编码转化为UTF-8
+ * 使用用commons-io.jar实现文件的读取和写入
+ * */
+
+public class EncodeTest1 {
+	public static void main(String[] args) throws IOException {
+		String srcDirPath = "/home/cwjy1202/hadoop/旅游领域/旅游类测试文档/地方文化";
+		// 转为UTF-8编码格式源码路径。这个路径可以若不存在,会自动建立。
+		String utf8DirPath = "/home/cwjy1202/hadoop/旅游领域/旅游类测试文档/地方文化1";
+
+		// 获取所有txt文件
+		Collection<File> javaGbkFileCol = FileUtils.listFiles(new File(srcDirPath), new String[] { "txt" }, true);
+
+		for (File javaGbkFile : javaGbkFileCol) {
+			// UTF8格式文件路径
+			String utf8FilePath = utf8DirPath + javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
+			// 使用GBK读取数据,然后用UTF-8写入数据
+			FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
+		}
+
+	}
+}

+ 38 - 0
php/convert.php

@@ -0,0 +1,38 @@
+<?php
+//解决eclipse编码GBK转换为utf-8编码问题
+//项目文件放在src文件夹
+//访问:http://others.yoqi.me/convert.php
+function listDir($dir)
+{
+	echo "start!";
+    if(is_dir($dir))
+    {
+        if ($dh = opendir($dir)) 
+        {
+            while (($file = readdir($dh)) !== false)
+            {
+                if((is_dir($dir."/".$file)) && $file!="." && $file!="..")
+                {
+                	//进入子文件夹
+                    rename($dir."/".$file,$dir."/".mb_convert_encoding($file,"GBK", "UTF-8"));
+                    listDir($dir."/".$file."/");
+                }
+                else
+                {
+                    if($file!="." && $file!="..")
+                    {
+                         rename($dir."/".$file,$dir."/".mb_convert_encoding($file,"GBK", "UTF-8"));
+						 echo $dir."/".$file;
+                    }
+                }
+            }
+            closedir($dh);
+        }
+    }
+	echo "finish!";
+}
+//开始运行
+
+listDir("E:/web/iis/teleport");
+
+?>

+ 39 - 0
python/convert.py

@@ -0,0 +1,39 @@
+# Python3 将GBK转换成utf-8编码,明天继续实现,把*.java文件 *.porperties文件都转成utf-8
+import os, chardet, codecs
+
+def ReadFile(filePath, encoding="gbk"):
+    with codecs.open(filePath, "r", encoding) as f:
+        return f.read()
+ 
+def WriteFile(filePath, u, encoding="utf-8"):
+    with codecs.open(filePath, "w", encoding) as f:
+        f.write(u)
+ 
+def GBK_2_UTF8(src, dst):
+#     检测编码
+    f = open(src, "rb")
+    coding = chardet.detect(f.read())["encoding"]
+    f.close()
+    if coding != "utf-8":
+        content = ReadFile(src, encoding=coding)
+        WriteFile(dst, content, encoding="utf-8")
+        try:
+            print(src + "  " + coding + " to utf-8  converted!")
+        except Exception:
+            print("print error")
+
+# 递归遍历rootdir目录,把目录中的*.java编码由gbk转换为utf-8
+def ReadDirectoryFile(rootdir):
+    for parent, dirnames, filenames in os.walk(rootdir):
+            # case 1:
+            for dirname in dirnames:
+#                     print("folder is:" + os.path.join(parent, dirname))
+                    ReadDirectoryFile(dirname)
+            # case 2
+            for filename in filenames:    
+#                     print("filename with full path:" + os.path.join(parent, filename))
+                    if filename.endswith(".java"):
+                            GBK_2_UTF8(os.path.join(parent, filename), os.path.join(parent, filename))
+if __name__ == "__main__":
+    src_path = "D:/liuyuqi/twtech/github/q 前端/java-note/notepadpp"
+    ReadDirectoryFile(src_path)