2

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 "??????".

Makoto
  • 104,088
  • 27
  • 192
  • 230
Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • Is the result of files[0] displayed on the console or in an output file (as junk)? – Ryan Fernandes Mar 11 '10 at 11:08
  • What Ryan Fernandes is hinting at is that there's a change that the directory name is read correctly by your program, but when you print the name out, the *console* will actually fail in displaying the characters correctly. – Thomas Mar 11 '10 at 11:16
  • @ryan...no...I am not printing it anywhere. I am simplying using the filename to open a stream to it like new FileInputStream(files[0])...and it gives me a FileNotFoundException because the filename is ???? – Suraj Chandran Mar 12 '10 at 01:21
  • @thomas....i have debugged and checked that the program gets the directory name as "?????" – Suraj Chandran Mar 12 '10 at 01:24
  • I understand the problem better now. I have updated my answer accordingly. Let me know if it helps. – Ryan Fernandes Mar 12 '10 at 04:41

3 Answers3

1

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

Inv3r53
  • 2,929
  • 3
  • 25
  • 37
0

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
  • 8,238
  • 7
  • 36
  • 53
  • 1
    @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 Mar 13 '10 at 03:51
0

It sounds like you are maybe hitting the same problem as http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4866151

Usually this is caused when the file is created with one encoding and then tried to read via another.

Justus
  • 203
  • 1
  • 4