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紧缩时也会带来乱码问题。