1

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!

Dimitri Bosteels
  • 381
  • 4
  • 12
  • If I choosed a file for write by a filechooser it's work. – István Lipcsei Mar 06 '17 at 14:14
  • 1
    Would you post stack trace and toValidFileName code? – bichito Mar 06 '17 at 14:16
  • Please chose the same file with the file-choser and print the file name. Compare the name to the one that fails. Be on the lookout for whitespace. –  Mar 06 '17 at 14:16
  • What does `System.out.println(filename.length());` print? I suspect you may have invisible control characters in your filename. – VGR Mar 06 '17 at 14:18
  • Could you please copy the path from the File explorer and pass it as a variable and see if it works ! If it works there is something wrong with your toValidFileName() if not its with the file or something else . – Raj Asapu Mar 06 '17 at 14:21

2 Answers2

0

Try using filewriter instead. Your try will look like this:

try {
     File dir = new File("C:/Users//" + userhome + "/Documents"); //customize this however
     FileWriter fstr = new FileWriter(new File(dir, rdp));
          for (int j = 0; j < 2336; j++) {
                c = ProgramList.get(item).p_body.charAt(j);
                fstr.write(c);
                        }
            fw.close(); 
        }

Also, try to stick to Java variable naming conventions. userhome should be userHome, etc. Just a minor thing :)

Jerevand
  • 9
  • 3
  • Why do you think that would help in this situation? – matt Mar 06 '17 at 14:27
  • Filewriter has proven itself to me as logical and straightforward. My hope is that the simplified input will resolve the issue, because my best bet is that their code isn't generating/reading the right paths. – Jerevand Mar 06 '17 at 15:12
0

The problem was generated in that line:

filename = toValidFileName(ProgramList.get(item).getP_name());

with the getP_name() results, becouse sometimes it has (char) 0 characters...

The code is rewritten -> the (char) 0 characters are changed to (char) 20 characters in the object and in this way everything is working.

The toValidFileName code anyway this:

public static String toValidFileName(String input) {
    return input.replaceAll("[:\\\\/*\"?|<>']", "_");
}

Thank you all for your help! There are some usefully advice especially Arkady's and VGR's advice!