3

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.

1 Answers1

1

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.

Community
  • 1
  • 1
Wivani
  • 2,036
  • 22
  • 28
  • I doubt Commons IO would help any on this occasion. – skaffman Aug 29 '11 at 15:10
  • Admittedly so. But Java's IO (in particular versions) has a bad reputation to say the least. Hence the advice. – Wivani Aug 29 '11 at 15:18
  • The path name is wrong, is like this: win - Path: C:\www\compartida\carpetón linux - /www/compartida/carpet� Using commons io from Apache, in windows works fine without doing any String conversion only using File objects. `FileInputStream fileInput = FileUtils.openInputStream(fileInDir[i]);` but in Linux enviroment I get `FileNotFoundException` for files containing some special characters in their name. Any suggestion? – Juan Arcadio Aug 30 '11 at 10:14
  • And what path is constructed in Linux (with or without Apache IO)? – Wivani Aug 30 '11 at 10:23
  • The path that I constructed doing String conversions is the same that Apache IO constructed in Linux. When it throws exception with my own path conversion print: `java.io.FileNotFoundException: /www/compartida/carpet� (No such file or directory)` When it throws exception with Apache IO print: `java.io.FileNotFoundException: File '/www/compartida/carpet�' does not exist` – Juan Arcadio Aug 30 '11 at 11:23
  • Thanks, I will discuss it with the administrators of the Linux System to test changing system locale. – Juan Arcadio Aug 30 '11 at 13:12