0

I'm trying to create a file and append some information.

First thing I do is creating the folders, then I create the files.

try {
        if (!new File("Results\\" + p.getName()).exists()) {
            new File("Results\\" + p.getName()).mkdir();
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error creating the directory");
    }


try {
    PrintWriter  writer = new PrintWriter ("Results\\" + p.getName() + "\\" + simulation.getNameSim(), "utf-8"); 
    //After this instruction jumps to exception

    //Code
    } catch (Exception e) {

    }

The variables p.getName and simulation.getName have the information. The debug says there are no erros creating the directory but after the print the program throws an exception.

Any ideas? I'm creating the files inside a java's program directory because I need to send the program to another persons and I guess its not about permissions.

Patrick Weiß
  • 436
  • 9
  • 23
Joseph
  • 113
  • 11

3 Answers3

0

You haven't showed us the error you're getting but here are a couple of points:

  1. File.mkdir() does not throw an exception in case the directory is not created (it returns false instead). So there is no point behind the catch that displays a message dialog.

  2. Since the directory path is actually composed of two folders, you need to call mkdirs() instead of mkdir(). The former will create any needed parent directories. In this case, it first creates the folder Results before creating the folder named by p.getName().

M A
  • 71,713
  • 13
  • 134
  • 174
0

this piece of code is working fine for me. It's basically your code with less stuff staying the same though:

public static void main(String[] args) {
    try {
        if (!new File("Results/anyfoldername" ).exists()) {
            new File("Results/anyfoldername" ).mkdir();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


try {
    PrintWriter  writer = new PrintWriter ("Results/anyfolder/anyfilename","utf-8"); 
    //After this instruction jumps to exception

    //Code
    } catch (Exception e) {
        e.printStackTrace();
    }

}

This might help you. You should try to use "/" and not "\" When using "\" I get an exception that it cant find the directory.

LOLWTFasdasd asdad
  • 2,625
  • 5
  • 26
  • 39
0

As the previous answer says there is not much to go on :)

But I would say you need something like this

if (!new File("Results\\" + p.getName()).exists()) {
    if (!new File("Results\\" + p.getName()).mkdir()) {
      JOptionPane.showMessageDialog(null, "Error creating the directory");
      return; 
   }
}

  //removed try / catch here for clarity
  File  file = new File("Results\\" + p.getName() + "\\" + simulation.getNameSim()).createNewFile(); 
  // I would then use the Buffered File answer as detailed here: http://stackoverflow.com/questions/1053467/how-do-i-save-a-string-to-a-text-file-using-java

It looks to me that you are using FileWriter and then will use FileWriter.println() but it's not clear from your snippet.

I would also use File.separator rather than \ in your file path.

Personally I'd always go for the BufferedReader/Writers

RNJ
  • 15,272
  • 18
  • 86
  • 131