Windows系統中編輯的Java源碼,在Linux下打開會出現中文亂碼的情況。緣由就是文件編碼格式的問題,Windows下通常是GBK而Linux下是UTF⑻。
在vim中用set fileencoding命令就能夠看出編碼格式,以下:
//linux下
fileencoding=utf-8
//windows下
fileencoding=latin1
最簡單的辦法就是在windows下將文件另存為utf8格式。那末在linux下我們可使用iconv工具將其轉換格式。
$ iconv --help
Usage: iconv [OPTION...] [FILE...]
Convert encoding of given files from one encoding to another.
Input/Output format specification:
-f, --from-code=NAME encoding of original text
-t, --to-code=NAME encoding for output
Information:
-l, --list list all known coded character sets
Output control:
-c omit invalid characters from output
-o, --output=FILE output file
-s, --silent suppress warnings
--verbose print progress information
-?, --help Give this help list
--usage Give a short usage message
-V, --version Print program version
$ iconv -f GBK -t UTF-8 test.java -o test2.java
轉換終了,中文亂碼就不見了。
這就要用到convmv工具了。
$ convmv
Your Perl version has fleas #22111 #37757 #49830
convmv 1.15 - converts filenames from one encoding to another
Copyright (C) 2003⑵011 Bjoern JACKE <bjoern@j3e.de>
USAGE: convmv [options] FILE(S)
-f enc encoding *from* which should be converted
-t enc encoding *to* which should be converted
-r recursively go through directories
-i interactive mode (ask for each action)
--nfc target files will be normalization form C for UTF-8 (Linux etc.)
--nfd target files will be normalization form D for UTF-8 (OS X etc.)
--qfrom be quiet about the "from" of a rename (if it screws up your terminal e.g.)
--qto be quiet about the "to" of a rename (if it screws up your terminal e.g.)
--exec c execute command instead of rename (use #1 and #2 and see man page)
--list list all available encodings
--lowmem keep memory footprint low (see man page)
--nosmart ignore if files already seem to be UTF-8 and convert if posible
--notest actually do rename the files
--replace will replace files if they are equal
--unescape convert%20ugly%20escape%20sequences
--upper turn to upper case
--lower turn to lower case
--parsable write a parsable todo list (see man page)
--help print this help
將tech目錄下的文件夾或文件遞歸轉換:
sudo convmv -f gbk -t utf-8 -r --notest tech/
另外需要注意,有時候在windows上用zip緊縮時也會帶來亂碼問題。