问题描述
我今天遇到了一个中文乱码的问题,虽然现在貌似解决了,但我想了很久没想明白。希望大家帮我支支招,让我搞明白这是啥原因。 我的疑问是我的java我的java源文件是用utf-8保存的在solaris机器上也是用utf-8进行编译的为什么执行的时候 String newp = new String(path.getBytes("utf-8"),encoding)生成的文件名反而是乱码 改成String newp = new String(path.getBytes("gbk"),encoding)就能显示正常的中文? 为什么?这个gbk是从哪来的? 太郁闷了,大家有知道的请告诉我,感谢! 代码如下 public static void main(String[] args) throws InterruptedException { System.getProperties().list(System.out); System.out.println("******************"); final String encoding = System.getProperty("file.encoding"); System.out.println("encoding:"+encoding); String path= "./哈haha哈AAA璎玥.txt"; System.out.println(path); // TODO file.encoding=iso8859-1 try { String newp = new String(path.getBytes("gbk"),encoding); System.out.println(newp); File file = new File(newp); boolean b = file.createNewFile(); System.out.println("file create:"+b); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } 代码的文件编码 在eclipse里面选的是utf-8 然后我将该文件ftp传到solaris机器上 查看solaris机器上jdk的版本$ java -versionjava version "1.5.0"Java(TM) 2 Runtime Environment, Standard Edition (build pap64dev-20100813 (SR12 FP1 ))IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64 j9vmap6423-20100808 (JIT enabled)J9VM - 20100629_60535_BHdSMrJIT - 20100623_16197_r8GC - 20100211_AA)JCL - 20100813 $ localeLANG="zh_CN.UTF-8"LC_COLLATE="C"LC_CTYPE="C"LC_MONETARY="C"LC_NUMERIC="C"LC_TIME="C"LC_MESSAGES="C"LC_ALL=$ 指定utf-8 对源文件进行编译 $ javac -encoding utf-8 FileTest.java 用javap查看一下$ javap -verbose FileTestCompiled from "FileTest.java"public class FileTest extends java.lang.Object SourceFile: "FileTest.java" minor version: 0 major version: 49 Constant pool:const #1 = Method #29.#40; // java/lang/Object."<init>":()Vconst #2 = Method #41.#42; // java/lang/System.getProperties:()Ljava/util/Properties;const #3 = Field #41.#43; // java/lang/System.out:Ljava/io/PrintStream;const #4 = Method #44.#45; // java/util/Properties.list:(Ljava/io/PrintStream;)Vconst #5 = String #46; // ******************const #6 = Method #47.#48; // java/io/PrintStream.println:(Ljava/lang/String;)Vconst #7 = String #49; // file.encodingconst #8 = Method #41.#50; // java/lang/System.getProperty:(Ljava/lang/String;)Ljava/lang/String;const #9 = class #51; // java/lang/StringBuilderconst #10 = Method #9.#40; // java/lang/StringBuilder."<init>":()Vconst #11 = String #52; // encoding:const #12 = Method #9.#53; // java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;const #13 = Method #9.#54; // java/lang/StringBuilder.toString:()Ljava/lang/String;const #14 = String #55; // ./?haha?AAA??.txtconst #15 = class #56; // java/lang/Stringconst #16 = String #57; // gbkconst #46 = Asciz ******************;const #47 = class #79; // java/io/PrintStreamconst #48 = NameAndType #80:#81;// println:(Ljava/lang/String;)Vconst #49 = Asciz file.encoding;const #50 = NameAndType #82:#83;// getProperty:(Ljava/lang/String;)Ljava/lang/String;public FileTest(); Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return LineNumberTable: line 6: 0 public static void main(java.lang.String[]) throws java.lang.InterruptedException; Code: Stack=4, Locals=6, Args_size=1 12: ldc #5; //String ****************** 14: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 17: ldc #7; //String file.encoding 19: invokestatic #8; //Method java/lang/System.getProperty:(Ljava/lang/String;)Ljava/lang/String; 22: astore_1 23: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream; 26: new #9; //class java/lang/StringBuilder 29: dup 30: invokespecial #10; //Method java/lang/StringBuilder."<init>":()V 33: ldc #11; //String encoding: 35: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 38: aload_1 39: invokevirtual #12; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 42: invokevirtual #13; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 45: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 48: ldc #14; //String ./?haha?AAA??.txt 50: astore_2 51: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream; 54: aload_2 55: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 58: new #15; //class java/lang/String 61: dup 62: aload_2 63: ldc #16; //String gbk 65: invokevirtual #17; //Method java/lang/String.getBytes:(Ljava/lang/String;)[B 68: aload_1 69: invokespecial #18; //Method java/lang/String."<init>":([BLjava/lang/String;)V // 太长显示不了,删除掉 } 运行代码 注意 我未指定 –Dfile.encoding=”utf-8”而是默认 iso8859-1$ java FileTest.The java class is not found: FileTest.$ java FileTest-- listing properties --java.assistive=ONjava.runtime.name=Java(TM) 2 Runtime Environment, Stand...ibm.signalhandling.rs=falsesun.boot.library.path=/usr/java5_64/jre/binjava.vm.version=2.3com.ibm.oti.configuration=scarjava.vm.vendor=IBM Corporationjava.vendor.url=http://www.ibm.com/path.separator=:java.vm.name=IBM J9 VMuser.country=USjava.vm.specification.name=Java Virtual Machine Specificationuser.dir=/data/zhjk/ISS_HOME/data/test-ouyjava.runtime.version=pap64dev-20100813 (SR12 FP1 )java.fullversion=J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64 j9...java.awt.graphicsenv=sun.awt.X11GraphicsEnvironmentos.arch=ppc64com.ibm.vm.bitmode=64java.io.tmpdir=/tmp/com.ibm.util.extralibs.properties=line.separator= java.vm.specification.vendor=Sun Microsystems Inc.user.variant=java.awt.fonts=os.name=AIXsun.java2d.fontpath=java.jcl.version=20100813sun.jnu.encoding=ISO8859-1java.library.path=/usr/java5_64/jre/bin:/usr/java5_64/j...jxe.current.romimage.version=9com.ibm.oti.vm.bootstrap.library.path=/usr/java5_64/jre/bincom.ibm.cpu.endian=bigjava.specification.name=Java Platform API Specificationjava.class.version=49.0ibm.system.encoding=ISO8859-1invokedviajava=java.util.prefs.PreferencesFactory=java.util.prefs.FileSystemPreferences...os.version=6.1com.ibm.oti.vm.library.version=23user.home=/data/zhjkuser.timezone=java.awt.printerjob=sun.print.PSPrinterJobjava.specification.version=1.5file.encoding=ISO8859-1java.class.path=.user.name=issjava.vm.specification.version=1.0java.home=/usr/java5_64/jresun.arch.data.model=64java.specification.vendor=Sun Microsystems Inc.ibm.signalhandling.sigint=trueuser.language=encom.ibm.oti.jcl.build=20100528_1023java.vm.info=J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64 j9...java.version=1.5.0java.ext.dirs=/usr/java5_64/jre/lib/extsun.boot.class.path=/usr/java5_64/jre/lib/vm.jar:/usr/jav...jxe.lowest.romimage.version=9java.vendor=IBM Corporationfile.separator=/java.compiler=j9jit23sun.io.unicode.encoding=UnicodeBigibm.signalhandling.sigchain=true******************encoding:ISO8859-1./?haha?AAA??.txt./哈haha哈AAA璎玥.txtfile create:true$ 结果 生成的文件名是中文的 ok 问题补充:<div class="quote_title">chen_yongkai 写道</div><div class="quote_div">你开发和编译的机器的系统编码是GBK吧</div><br /><br />我是在Windows xp的环境下编写的java文件<br />文件保存格式在eclipse里面设置的是 utf-8
解决方案
你是用的ssh到linux去执行编译的吧,这个ssh客户端也是有编码指定的,应该是你的这个编码和系统不一致导致的。如果你在编译的时候不指定编码的话,生成的文件名应该有问题吧。
解决方案二:
不行再试试用native2asciiC:Usersj>native2ascii中文文件名u4e2du6587u6587u4ef6u540dFile file = new File("./u4e2du6587u6587u4ef6u540d.txt"); boolean b = file.createNewFile();
解决方案三:
直接用File file = new File("./哈haha哈AAA璎玥.txt"); boolean b = file.createNewFile(); 应该就可以了
解决方案四:
你开发和编译的机器的系统编码是GBK吧