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(".")