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.