honor571
2012-03-13 23:21
浏览 610
已采纳

linux下File.createNewFile(“中文文件名”) 乱码问题

我今天遇到了一个中文乱码的问题,虽然现在貌似解决了,但我想了很久没想明白。希望大家帮我支支招,让我搞明白这是啥原因。

 

我的疑问是

我的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 -version

java 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_BHdSMr

JIT  - 20100623_16197_r8

GC   - 20100211_AA)

JCL  - 20100813

 

 

 

 



$ locale

LANG="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 FileTest

Compiled 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>":()V

const #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;)V

const #5 = String       #46;    //  ******************

const #6 = Method       #47.#48;        //  java/io/PrintStream.println:(Ljava/lang/String;)V

const #7 = String       #49;    //  file.encoding

const #8 = Method       #41.#50;        //  java/lang/System.getProperty:(Ljava/lang/String;)Ljava/lang/String;

const #9 = class        #51;    //  java/lang/StringBuilder

const #10 = Method      #9.#40; //  java/lang/StringBuilder."<init>":()V

const #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??.txt

const #15 = class       #56;    //  java/lang/String

const #16 = String      #57;    //  gbk

const #46 = Asciz       ******************;

const #47 = class       #79;    //  java/io/PrintStream

const #48 = NameAndType #80:#81;//  println:(Ljava/lang/String;)V

const #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=ON

java.runtime.name=Java(TM) 2 Runtime Environment, Stand...

ibm.signalhandling.rs=false

sun.boot.library.path=/usr/java5_64/jre/bin

java.vm.version=2.3

com.ibm.oti.configuration=scar

java.vm.vendor=IBM Corporation

java.vendor.url=http://www.ibm.com/

path.separator=:

java.vm.name=IBM J9 VM

user.country=US

java.vm.specification.name=Java Virtual Machine Specification

user.dir=/data/zhjk/ISS_HOME/data/test-ouy

java.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.X11GraphicsEnvironment

os.arch=ppc64

com.ibm.vm.bitmode=64

java.io.tmpdir=/tmp/

com.ibm.util.extralibs.properties=

line.separator=

 

java.vm.specification.vendor=Sun Microsystems Inc.

user.variant=

java.awt.fonts=

os.name=AIX

sun.java2d.fontpath=

java.jcl.version=20100813

sun.jnu.encoding=ISO8859-1

java.library.path=/usr/java5_64/jre/bin:/usr/java5_64/j...

jxe.current.romimage.version=9

com.ibm.oti.vm.bootstrap.library.path=/usr/java5_64/jre/bin

com.ibm.cpu.endian=big

java.specification.name=Java Platform API Specification

java.class.version=49.0

ibm.system.encoding=ISO8859-1

invokedviajava=

java.util.prefs.PreferencesFactory=java.util.prefs.FileSystemPreferences...

os.version=6.1

com.ibm.oti.vm.library.version=23

user.home=/data/zhjk

user.timezone=

java.awt.printerjob=sun.print.PSPrinterJob

java.specification.version=1.5

file.encoding=ISO8859-1

java.class.path=.

user.name=iss

java.vm.specification.version=1.0

java.home=/usr/java5_64/jre

sun.arch.data.model=64

java.specification.vendor=Sun Microsystems Inc.

ibm.signalhandling.sigint=true

user.language=en

com.ibm.oti.jcl.build=20100528_1023

java.vm.info=J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64 j9...

java.version=1.5.0

java.ext.dirs=/usr/java5_64/jre/lib/ext

sun.boot.class.path=/usr/java5_64/jre/lib/vm.jar:/usr/jav...

jxe.lowest.romimage.version=9

java.vendor=IBM Corporation

file.separator=/

java.compiler=j9jit23

sun.io.unicode.encoding=UnicodeBig

ibm.signalhandling.sigchain=true

******************

encoding:ISO8859-1

./?haha?AAA??.txt

./哈haha哈AAA璎玥.txt

file create:true

$

 

 

结果 生成的文件名是中文的 ok



 

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题