FileNotFoundException when trying to read the listFiles that system provides me. Is it a name encoding problem?
I'm using tomcat 5.5.28 in a windows machine with -Dfile.encoding=UTF-8 in JAVA_OPTS.
I have a problem reading files from the file system, this is my code:
File directory = new File(directoryPath);
if (directory.exists()) {
File[] fileInDir = directory.listFiles();
for (int i=0; i<fileInDir.length; i++) {
FileInputStream fileInput = new FileInputStream(fileInDir[i]);
...
}
}
It works fine if the file doesn't contain any "strange" character. If the directory contains a file with acute/tilde whithin his name, when I try to create the FileInputStream I get FileNotFoundException.
I solve it using a decoded String instead of a File object, doing this:
String name = new String(fileInDir[i].getName().getBytes(), System.getProperty("file.encoding"));
String parent = new String(fileInDir[i].getParent().getBytes(), System.getProperty("file.encoding"));
Charset systemCharset = Charset.forName(System.getProperty("file.encoding"));
CharsetDecoder systemDecoder = systemCharset.newDecoder();
CharBuffer cbufN = systemDecoder.decode(ByteBuffer.wrap(name.getBytes()));
CharBuffer cbufP = systemDecoder.decode(ByteBuffer.wrap(parent.getBytes()));
String path = cbufP.toString() + File.separator + cbufN.toString();
FileInputStream fileInput = new FileInputStream(path);
It works in my windows machine, I can read files like (X:\directory\zípìç\ñañaf.txt) without problems:
I moved this code to other enviroment: a linux machine with same tomcat version (5.5.28), same java virtual machine version (1.6.0_20), same file.encoding option (UTF-8),... and开发者_开发百科 I get again FileNotFoundException.
Am I doing something wrong?
Thanks for any assistance. Juan Arcadio.
Have you tried looking at the path you construct in your workaround code (in both environments)?
System.out.println("Path: "+path);
Other than that I would advice to make use of an API like Apache Commons IO or similar.
EDIT:
If I'm not mistaken you're question is related to this one. See if the workaround in there helps (changing your unix/linux system's locale).
And if you have anything to say in this I would always advice against using spaces and special characters/glyphs like tilde etc in filenames. From the looks of it it's about a www (web) folder; in that case using spaces etc is pure madness.
精彩评论