Java FileWriter. How do you create a folder in the same directory as your .exe file?

huangapple go评论69阅读模式
英文:

Java FileWriter. How do you create a folder in the same directory as your .exe file?

问题

public void saveFile() {
    try {
        try (BufferedWriter save = new BufferedWriter(new FileWriter("Lessons\\" + tempTextField.getText() + ".txt"))) {
            save.write(lessonPane.getText());
            JOptionPane.showMessageDialog(null, "File Saved!");
        }
    } catch (IOException e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

这是您目前的代码,目前它运行得很好。但要使其工作,您必须在项目目录中创建一个名为"Lessons"的文件夹,否则它会报告找不到位置的错误。

问题在于,我想为这个项目制作一个.exe文件和安装程序(因为这是学校项目的要求)。在构建项目并使用L4J创建.exe文件以及使用ISC创建安装程序后,它会报告目标位置(即文件夹"Lessons")无效/丢失的错误。

在尝试解决这个问题后,我认为它不再工作的原因是,我的.jar文件、.exe文件和JRE位于"dist"文件夹中(在我清理和构建项目以获取.jar文件后生成),而文件夹"Lessons"位于根目录中。

如何使文件夹在与.jar或.exe相同的位置自动生成,我不确定是否可能。是否有其他解决这个问题的方法?

非常感谢!

英文:
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!

答案1

得分: 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"))) {
        save.write(lessonPane.getText());
        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 don't know how or why it worked. But it worked.

英文:
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.

huangapple
  • 本文由 发表于 2020年8月5日 11:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63258177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定