Windowsマシンでtomcat 5.5.28を使用していますが、JAVA_OPTSで-Dfile.encoding = UTF-8を使用しています。FileNotFoundExceptionシステムが提供するlistFileを読み込もうとしています。それは名前のエンコードの問題ですか?
私は、これは私のコードで、ファイルシステムからのファイルの読み取りに問題があります。
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]);
...
}
}
ファイルは任意の「奇妙な」の文字が含まれていない場合、それは正常に動作します。 ディレクトリにファイル名が含まれている場合、FileInputStreamを作成しようとするとFileNotFoundExceptionが発生します。
私はこれをやって、代わりにFileオブジェクトのデコードされた文字列を使用してそれを解決:\ディレクトリ\zípìç\ñañaf.txt:
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);
それは私のWindowsマシンで動作しますが、私は(Xのようなファイルを読むことができます他の環境に移しました:同じTomcatバージョン(5.5.28)、同じJava仮想マシンバージョン(1.6.0_20)、同じfile.encodingオプション(UTF-8)を持つLinuxマシン。 、...そして私は再びFileNotFoundExceptionを取得します。
何か間違っていますか?
ありがとうございます。 Juan Arcadio:
私はCommons IOがこの機会に助けになるとは思っていません。 – skaffman
そうです。しかし、JavaのIO(特にバージョン)には、ほとんど言わない評判が悪い。したがってアドバイス。 – Wivani
パス名は、間違っている、このようなものです: 勝利 - パス:C:\ WWW \ compartida \ のlinuxcarpetón - コモンズの使用/ WWW/compartida /カーペット IOのApacheから、ウィンドウ内には、いずれかを行うことなく正常に動作します文字列変換は、Fileオブジェクトを使用してのみ行います。 'FileInputStream fileInput = FileUtils.openInputStream(fileInDir [i]);' Linux環境では、名前に特殊文字を含むファイルに対して 'FileNotFoundException'を取得します。なにか提案を? –