I’m getting the
java.io.IOException: The filename, directory name, or volume label syntax is incorrect
and I can’t see why.
If I try my code directly with a string, it works (folder exists, permissions ok, etc.)
When I try to build the string from an array, it fails with the exception above.
Here’s the code, with commented lines that I’ve tried that fail and what works as well as what the println output is:
// //////////////////////////////////////////////////////////////////
// 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);
}
}
Can anyone see why this is happening?
Thanks.
try
File targetFile = new File(dlDirectory, filesToRetrieve[i]);P.S.
maybe try with
trim()file name.