views:

196

answers:

3

I am running a Java application on a Solaris10 with Chinese. Now there are some files in a directory with chinese filenames. When I do files = new File(dir).list() where "dir" is the parent directory containing that chinese file, I get the result filename files[0] as ?????(some junk characters).

Now the deal is that my programs file.encoding property is already set to GBK and I also do Charset.isSupported("GBK") and it returns true too. So where could be the problem. I am running out of ideas.

NOTE: I am not trying to print the filename anywhere or copy the file or something. I am simply openeing a stream to it, something like below:

files = new File(dir).list();
new FileInputStream(files[0]);

Now this gives me a FileNotFoundExcpetion, so I debug just to find that value inside files[0] is "??????".

A: 

I guess this problem has nothing to do with Java side. Maybe you could try to change your system locale. check the conf file:

/etc/sysconfig/i18n

backup your old conf, try with following settings:

LANG="zh_CN.GBK"
LANGUAGE="zh_CN.GBK:zh_CN.GB18030:zh_CN.GB2312:zh_CN"
SUPPORTED="zh_CN.GB18030:zh_CN:zh:en_US.UTF-8:en_US:en"
SYSFONT="lat0-sun16" 

You may need to do a reboot to make the change come into effect.

hope it works.

Kent
@kent..I have no directory /etc/sysconfig
Suraj Chandran
I cannot help you with Solaris, but I support Kent: your problem is not Java-related. It's a setting in your OS.
dimitko
I tried exporting these environ variables to the specified values. But the problem persists.
Suraj Chandran
+1  A: 

Not sure if it a good practice of doing it . try setting the charset when you launch the jvm using : java -Dfile.encoding="" ...

daedlus
As I have mentioned in the question, I have already set the encoding as GBK from the command line
Suraj Chandran
A: 

ok can you try this instead

//String[] files = new File(dir).list(); 
File[] files = new File(dir).listFiles(); //use 'File' references instead.
FileInputStream fos = new FileInputStream(files[0]);

This removes the dependencies on file names and works directly with the File object.

Ryan Fernandes
@ryan...well, it wont work either, because listFiles() essentially does the same thing internally i.e. use the list() method and then create a File object for each filename returned. So the exception will still come, but only now it will move two layers up in the stack frame. Nice attempt though:)
Suraj Chandran