My application throws an exception - java.io.FileNotFoundException: Invalid file path. Not sure why. I've read the questions and answers about the theme but no one could help me.
Here is the code:
String userhome = System.getProperty("user.home");
String filename = null;
File rdp = null;
for (int item = 0; item < darab; item++) {
filename = toValidFileName(ProgramList.get(item).getP_name());
filename += ".rdp";
rdp = new File(userhome, filename);
try {
JFrame panel;
panel = new JFrame();
panel.setSize(400, 10);
panel.setLocation(300, 400);
panel.setTitle("Saving " + rdp.getAbsolutePath());
try (FileOutputStream fstr = new FileOutputStream(rdp)) {
panel.setVisible(true);
char c;
for (int j = 0; j < 2336; j++) {
c = ProgramList.get(item).p_body.charAt(j);
fstr.write(c);
}
fstr.flush();
fstr.close();
panel.setVisible(false);
}
} catch (IOException ioe) {
JOptionPane.showMessageDialog(this,
ioe.getMessage(), "Save rdp file", JOptionPane.ERROR_MESSAGE);
System.err.println(ioe.getMessage() + " : "+ rdp.getAbsoluteFile());
}
}
And the result: Invalid file path : C:\Users\LiPI\CosmicLd.rdp
toValidFilename() is remove the forbidden characters from the (KORG RADIAS) program name to create a valid file name.
I've not found my fault :( The destination directory is not read only, the user has the necessary privilegs. When I view the file.canWrite() after the line: rdp = new File (userhome, filename); it's always false. What did I do wrong? Thanks!