Linux 中 Windows 中文乱码

简介: Linux 下 Windows 源代码中文乱码由于 windows 和 linux 对文本的编码方式不同,所以经常会有 windows 中生成的文本在 linux 中打开乱码的情况。比如:我面临的情况是现在有很多的源代码文件是处于这样的情况,如下图:其中每个文件夹中包含了 java 源代码文件和子文件夹。

Linux 下 Windows 源代码中文乱码

由于 windows 和 linux 对文本的编码方式不同,所以经常会有 windows 中生成的文本在 linux 中打开乱码的情况。

比如:
img_f9c5edfcf12c3f4497596c36e5acc8d3.png

我面临的情况是现在有很多的源代码文件是处于这样的情况,如下图:
img_0fc71ee35eb97385873eb60ca9927282.png

其中每个文件夹中包含了 java 源代码文件和子文件夹。

在参考了网上的部分文章后(https://blog.csdn.net/imyang2007/article/details/7448177),我写了一个脚本用于将这个目录下的所有 java 源代码转码成 UTF-8。

后来转念一想,顺便扩展一下直接支持多种类型的源代码文件好了。

于是,就再次完善了一下。

对于我的情况,直接将脚本拖到 Tomcat-Java-Web-development-src-master_UTF-8 目录下,在命令行中运行即可(默认 java 源代码文件)

转码之后的结果:
img_f414835568dbba095f1dc8c002363513.png

使用方法

直接将脚本放到要进行转码的文件夹中,运行即可。

程序接受两个参数或者没有参数(默认当前文件夹,java 源代码),第一个是要进行转码的文件目录,第二个是源代码文件类型。

源代码如下:

import os
import sys

rootPath = './'
fileType = 'java'
if len(sys.argv) == 2:
    print("Two or zero argumens are needed \n    python GbToUFT.py [path] [fileType] \n")
    sys.exit(0)
elif len(sys.argv) == 3:
    rootPath = sys.argv[1]
    fileType = sys.argv[2]

for folderName,_,fileNames in os.walk(rootPath):
    javaFileList = [fileName for fileName in fileNames if fileName.endswith('.' + fileType)]
    for items in javaFileList:
        print(items)
        oldName = items
        tempName = items[0:len(items)-5] + "_." + fileType
        os.system("mv " + folderName + "/" + items + " " + folderName + "/" + tempName)
        os.system("iconv -f gbk -t utf8 " + folderName + \
         "/" + tempName + " > "  + folderName + "/" + oldName)
        os.system("rm -rf " + folderName + "/" + tempName)
AI 代码解读

GBToUTF.py

警告:对同一文件重复转码可能毁坏原文件,转码前请先备份。

原创文章,转载请注明出处。

参考文章

https://blog.csdn.net/imyang2007/article/details/7448177

目录
打赏
0
0
0
0
1
分享
相关文章
Shotcut 25.03 (Linux, macOS, Windows) - 免费开源视频编辑器
Shotcut 25.03 (Linux, macOS, Windows) - 免费开源视频编辑器
161 11
|
1月前
|
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
127 21
Studio 3T 2025.10 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
2月前
|
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
66 5
Metasploit Pro 4.22.7-2025052201 (Linux, Windows) - 专业渗透测试框架
Immunity CANVAS Professional 7.27 (macOS, Linux, Windows) - 渗透测试和漏洞利用平台
Immunity CANVAS Professional 7.27 (macOS, Linux, Windows) - 渗透测试和漏洞利用平台
86 3
Immunity CANVAS Professional 7.27 (macOS, Linux, Windows) - 渗透测试和漏洞利用平台
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
88 2
Flawnter 5.9.1 (macOS, Linux, Windows) - 应用程序安全测试软件
|
2月前
|
Nexpose 8.7.0 for Linux & Windows - 漏洞扫描
Nexpose 8.7.0 for Linux & Windows - 漏洞扫描
57 4
Nexpose 8.7.0 for Linux & Windows - 漏洞扫描
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架
49 4
Metasploit Pro 4.22.7-2025051201 (Linux, Windows) - 专业渗透测试框架
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
64 4
Metasploit Framework 6.4.63 (macOS, Linux, Windows) - 开源渗透测试框架
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
668 4
SecureCRT & SecureFX 9.6.3 for macOS, Linux, Windows - 跨平台的多协议终端仿真和文件传输
从Linux到Windows:阿里云服务器系统镜像适配场景与选择参考
阿里云为用户提供了丰富多样的服务器操作系统选择,以满足不同场景下的应用需求。目前,云服务器的操作系统镜像主要分为公共镜像、自定义镜像、共享镜像、镜像市场和社区镜像五大类。以下是对这些镜像类型的详细介绍及选择云服务器系统时需要考虑的因素,以供参考。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问