-1

java.io.IOExceptionが発生します:ファイル名、ディレクトリ名、またはボリュームラベルの構文が正しくないため、理由がわかりません。

文字列を使用してコードを直接試してみると、機能します(フォルダーが存在する、アクセス許可はOKなど)。配列から文字列を作成しようとすると、上記の例外を除いて失敗します。これがコードで、私が試したコメント行が失敗し、何が機能し、println出力が何であるかを示しています。

            // //////////////////////////////////////////////////////////////////
            // Create a file chooser and select a directory
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fc.setAcceptAllFileFilterUsed(false);

            int rVal = fc.showOpenDialog(MyApp.this);

            if (rVal == JFileChooser.APPROVE_OPTION) {
                dlDirectory = fc.getSelectedFile().toString() + "\\"; 
                System.out.println("Selected Directory: " + dlDirectory);
            } else {
                System.out.println("No Selection");
            }

            ...
            ...(I create a string array of file names here)
            ...

            for (int i = 0; i < filesToRetrieve.length; i++) {

                    //put together the directory and file name
                    String dlFileName = (dlDirectory + filesToRetrieve[i]); 
                    try {   
                                System.out.println(dlDirectory); // output: C:\Users\michael\Documents\tmp\ (as expected)
                                System.out.println(filesToRetrieve[i]); // output: nameoffile.txt (as expected)
                                System.out.println(dlFileName); // output: C:\Users\michael\Documents\tmp\nameoffile.txt (as expected)

                                File myFile = new File(dlFileName); //<--this does not work -- java.io.IOException: The filename, directory name, or volume label syntax is incorrect
                                //File myFile = new File(dlDirectory + filesToRetrieve[i]); //<--this does not work either

                                //File myFile = new File(dlDirectory + "nameoffile.txt");  // <--this does work !?!?

                                if(!myFile.exists()) {
                                    System.out.println("file does not exist");
                                    myFile.createNewFile();
                                } 
                    } catch (Exception e) {
                        System.err
                                .println("failed");
                        System.err.println(e);
                    }       
            }

なぜこれが起こっているのか誰かがわかりますか?ありがとう。

4

1 に答える 1

1

試すFile targetFile = new File(dlDirectory, filesToRetrieve[i]);

PSはおそらくtrim()ファイル名で試してみてください。

于 2013-02-03T05:26:36.537 に答える