0

I'm trying to save some output files at the same place as my Java classes. I want to use a relative path so there isn't any problem when I move the code somewhere else.

For the moment it writes my file under /home/myUser/output_j.erome and the actual code is :

FileWriter out = new FileWriter("output_j.erome",true);

I can't find how to create dynamically the folder at the same place as my Java classes and then add my file in it.

J.erome
  • 688
  • 7
  • 26
  • 1
    Does this answer your question? [How to get the path of a running JAR file?](https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file). See also https://stackoverflow.com/questions/227486/find-where-java-class-is-loaded-from – Joakim Danielson Mar 16 '20 at 08:25
  • 1
    [Getting the current working directory](https://stackoverflow.com/a/7603444/8498513) – donquih0te Mar 16 '20 at 08:27
  • 1
    "The same place" doesn't have a clearly-defined meaning. You're better off either using `user.home` if these are preferences or taking a property or command-line argument otherwise. – chrylis -cautiouslyoptimistic- Mar 16 '20 at 08:27
  • @chrylis-onstrike- with `the same place` I mean, I have a project with 2 levels of directories : `my project` - `src` - - `here I want my output folder` – J.erome Mar 16 '20 at 08:28
  • When you create a JAR, this would be inside the JAR and not writeable. – Nicktar Mar 16 '20 at 08:30
  • If you read the question carefully, OP is not asking for the location of the class files but the Java-source code. The suggested alternative answer is thus unrelated to the problem. While what OP is asking is not actually a very good idea, closing the question with a non-related answer is not the correct action. – Torben Mar 16 '20 at 08:42
  • 1
    Get the project path: `String projectPath = System.getProperty("user.dir");`. Add your desired directories: `String savePath = projectPath + "\\src\\My_Folder";`. If they don't exist make them: `File f = new File(savePath); if (!f.exists()) { f.mkdirs();}`. Now you have your save path created and ready to accept files. – DevilsHnd - 退職した Mar 16 '20 at 09:19
  • Will you only run your code from the IDE? What should when you build a runnable jar and installs it somewhere outside your project directory? – Joakim Danielson Mar 16 '20 at 10:01
  • @JoakimDanielson only from the IDE it's not going to be a jar – J.erome Mar 17 '20 at 09:02
  • @DevilsHnd it didn't work for me. I mean I still don't see a new folder in my IDE project – J.erome Mar 17 '20 at 09:06
  • One solution is to have your own properties file and have a property that points to the root of the project or the source folder and then you have a relative path in your code from there. So if you move your project all you have to do is to update the path in the properties file. It's not an automatic solution but it is a quite common one. – Joakim Danielson Mar 17 '20 at 09:17
  • yes that seems to be the solution for my problem – J.erome Mar 17 '20 at 10:03

0 Answers0