Mac OS 批量将Java编码iso-8859-1( english_us8859)转换为utf-8格式
!/bin/bash # 指定源目录 SOURCE_DIR="./serialMonitor" # 遍历源目录下所有.java文件 function convert_to_utf8() { local file="$1" encoding=`file -I ${file} | awk -F= '{print $2}'` echo "encoding: $encoding" if [[ "$encoding" == *"iso-8859-1"* ]]; then # 执行转换,输出为.utf8.txt文件 如果直接用iconv -f UTF-8 -t UTF-8 "$file" > "$file" 会导致文件内容被清空 iconv -f GBK -t UTF-8 "$file" > "${file}.utf8.txt" # 所以要将utf8.txt文件重命名为原文件名 mv "${file}.utf8.txt" "${file}" else echo "The file is already in UTF-8 encoding." fi } # 递归函数处理目录下的所有文件 function recursive_convert() { for entry in "$1"/*; do if [ -d "$entry" ]; then recursive_convert "$entry" elif [ -f "$entry" ]; then # 临时过滤jsp文件 if [[ "$entry" == *.java ]]; then convert_to_utf8 "$entry" fi fi done } recursive_convert $SOURCE_DIR