问题
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 "??????".
回答1:
Not sure if it a good practice of doing it . try setting the charset when you launch the jvm using : java -Dfile.encoding="" ...
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/2423781/chinese-encoding-issue-while-listing-files