-1
public void saveFile() {
        
         try {
             try (BufferedWriter save = new BufferedWriter (new FileWriter("Lessons\\" + tempTextField.getText() + ".txt"))) { // creates the file
                 save.write(lessonPane.getText()); // saves the contents of the text pane into file
                 JOptionPane.showMessageDialog(null, "File Saved!");
             }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e);
        }

this is what I currently have, and right now it's working just fine. But for this to work, I have to create a folder in the project directory or else it will throw me an error, saying the location is missing.

the problem is that I want to make an .exe file and installer for this project (because its a project requirement in school). And after I build, and make .exe using L4J, and installer using ISC, it will throw me an error saying that the target location (aka. the folder "Lessons") is invalid / missing.

after trying to solve it, I think the reason that its no longer working is because, my .jar .exe. and the JRE are in the folder "dist" (generated after I clean and build the project to get the .jar file) while the folder "Lessons" is on the root folder.

How can I make it that the folder will be automatically generated on the same location as the .jar or .exe. Honestly, I dont even know if that's possible. And is there any other way to work around this problem?

Thanks a lot!

  • 2
    You can use `Files.createDirectory` or `File.mkdir` to create a directory. However, you / your application may need elevated permissions to update the root directory. – Stephen C Aug 05 '20 at 04:00
  • thank you for your response! but it only solves half of the problem. I still need to find out how to create the directory in the same place as my .exe after I install this program – itskenleton Aug 05 '20 at 04:13
  • Why you have no catch for the inner try. – Marichyasana Aug 05 '20 at 04:13
  • 1
    @Marichyasana Because the inner try block is a try-with-resources, which will ensure that all resources are safely closed at the end of the try block. Any exceptions will be caught by the catch block on the outer try block. The OPs code is the same as if there was only one try block and it had both the try-with-resources and the catch block attached. – Charlie Armstrong Aug 05 '20 at 04:22
  • @CharlieArmstrong thx, I only used Java 6, so it is new to me. – Marichyasana Aug 05 '20 at 04:29
  • *"I still need to find out how to create the directory in the same place as my .exe after I install this program"* - You could either get the installer to do it or get the application itself to do it. But I don't know what L4J or ISC are referring to. Perhaps you could add the relevant tags to the question? Or use the full tool names rather than abbreviations? – Stephen C Aug 05 '20 at 04:32
  • @StephenC, sorry about that, Launch4j (L4J) is what I used to get the .exe file from the .jar. InnoSetup Compier (ISC) is what I used to create the installer. I am trying to get the application to do it automatically, wherever the user wants to install the program into, i want the application to adjust to that path – itskenleton Aug 05 '20 at 04:38
  • 1
    [InnoSetup Compiler documentation](https://jrsoftware.org/ishelp/topic_dirssection.htm) explains how to tell the installer to create extra directories. It looks like it will do what you want. – Stephen C Aug 05 '20 at 04:54

1 Answers1

0
public void saveFile() {
        
        File fdir = new File("Lessons");
        if (!fdir.exists()) {
            if(fdir.mkdirs()){
                System.out.println("directory created");
            } else {
                System.out.println("folder creation failed");
            }
        } else {
            System.out.println("directory already exists");
        }
        
         try {
             try (BufferedWriter save = new BufferedWriter (new FileWriter("Lessons\\" + tempTextField.getText() + ".txt"))) { // creates the file
                 save.write(lessonPane.getText()); // saves the contents
                 JOptionPane.showMessageDialog(null, "File Saved!");
             } 
        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, e);
        }

this is just silly, I just removed the slashes from the File and it worked. I dont know how or why did it worked. But it worked.