GBK编码的java文件批量转换为utf-8
eclipse workspaces中的项目有的是GBK编码,如果导入到workspaces的默认编码为utf-8编码的话中文会出现乱码。
下面python3的方式递归变量当前目录以及子目录,把目录中的*.java文件由gbk转换为utf-8,注意只能用一次,一次之后
当前目录以及子目录下的文件编码均会由gbk转为utf-8。
# Python3 将GBK转换成utf-8编码,明天继续实现,把*.java文件 *.porperties文件都转成utf-8
import 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 UTF8_2_GBK(src,dst):
content = ReadFile(src,encoding="gbk")
WriteFile(dst,content,encoding="utf-8")
import os
import os.path
# 递归遍历rootdir目录,把目录中的*.java编码由gbk转换为utf-8
def ReadDirectoryFile(rootdir):
for parent,dirnames,filenames in os.walk(rootdir):
#case 1:
for dirname in dirnames:
print("parent folder is:" + parent)
print("dirname is:" + dirname)
#case 2
for filename in filenames:
print("parent folder is:" + parent)
print("filename with full path:"+ os.path.join(parent,filename))
if filename.endswith(".java"):
UTF8_2_GBK(os.path.join(parent,filename),os.path.join(parent,filename))
print("Java文件")
if __name__=="__main__":
ReadDirectoryFile(".")