0

I just finished a little game and now I want to export it, so that I can use it as a normal application, which I can start from my desktop. It is working, but when it tries to read the .txt files, where I want to save a few important things, it isn't working anymore. But in the IDE it is working. I tested it with Eclipse and IntelliJ, but I get the same result.

programm structure

That's how I tried to save all of the things. the pictures are in folders in the res folder, which is in the src folder. this is working fine. I tried the same thing with the file in the saves folder, which also is in the src folder, but they aren't working after export.

BufferedReader read = new BufferedReader(new FileReader(getClass().getResource("/res/player/" + name + "/data.txt").getFile()));

That's the code with that I tried to read the text files

I didn't find any videos or other information in the internet, how to solve this problem.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
carlos.gmz
  • 100
  • 8
  • A "embedded resource" can not be treated like a file, instead you need to use the resulting `URL` directly to read the resource – MadProgrammer Aug 17 '20 at 23:00
  • @MadProgrammer oh wow, it is working perfect, that was the first time I was using stack overflow, I think, I am going to use it more now, that was really helpful. Thanks – carlos.gmz Aug 17 '20 at 23:20
  • This is a rather common issue, but unless you understand what it is, it's not easy to find a solution to. Glad we could help – MadProgrammer Aug 17 '20 at 23:22

2 Answers2

-1

You can try to pass the full length path to your BufferedReader.

Out of the blue:

String path = System.getProperty("java.class.path");

Then build your location from there. If that does not work, have a look here:

[https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file][1]

Then just try

BufferedReader bin = new BufferedReader(NewFileInputStream( [path] ) );

Oh, and also, it maybe faster for you to look directly into the api instead of searching for videos and tutorials. Those tend to run for half a hour and in the end you wasted your time when you in reality only needed 1 line of code.

https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html
clockw0rk
  • 576
  • 5
  • 26
-1

the pictures are in folders in the res folder, wich is in the src folder.

This suggests that when you "export" the project, the contents of the res folder is been bundled into the resulting Jar.

This is an approach commonly known as "embedded resources", where the resources are "embedded" into the application/Jar.

Since a Jar is just a zip file, you won't be able to access these resources as if they were files on the disk, because they aren't.

Instead, you'll need to make use of the resulting URL directly or use Class#getResourceAsStream, depending on your needs.

This means, that instead of...

BufferedReader read = new BufferedReader(new FileReader(getClass().getResource("/res/player/" + name + "/data.txt").getFile()));

you should be using something more like...

BufferedReader reader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/res/player/" + name + "/data.txt")));

I also hope you're making use of try-with-resources to manage you resources

Also, note, that embedding resources like this makes them readonly.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • does that mean, I cant write text into the files anymore? where should I save them otherwise – carlos.gmz Aug 17 '20 at 23:43
  • Okay, that's a much more complicate question. For [example](https://stackoverflow.com/questions/53405312/java-reading-the-properties-file-outside-of-jar-file/53405540#53405540), [example](https://stackoverflow.com/questions/58963290/how-to-import-a-text-file-in-netbeans/58963661#58963661), [example](https://stackoverflow.com/questions/53974191/get-current-path-of-executed-file/53982349#53982349) – MadProgrammer Aug 17 '20 at 23:54
  • Essentially, each OS has a concept of "application support", where applications can easily write data to, typically based on the current user. This makes the location "stable" and relatively easy to access and avoids many of the security pit falls OS's are now applying – MadProgrammer Aug 17 '20 at 23:57
  • so should I let the programm create the folders and text files, once it is startet the first time on the computer and then I can save them in the AppData Folder, because the User cant delete ore change it on accident? – carlos.gmz Aug 18 '20 at 08:36
  • Ahh, that's a decision you need to make. You could "extract" the resources you've embedded and store them in a "well known location" or, if it doesn't matter, create them when the program is first started, which you choose will be dependent on what you want to achieve – MadProgrammer Aug 18 '20 at 09:31
  • ok, thanks. I think I now have an idea what I have to do – carlos.gmz Aug 18 '20 at 09:33
  • Would any one care to highlight th issue with the answer and in what ways it doens't answer the user's question or would otherwise be a bad recommendation? – MadProgrammer Aug 18 '20 at 22:08
  • what haha, what do you mean – carlos.gmz Aug 21 '20 at 11:33
  • The answer was downvoted by some one and I'm curious to know why and how it might be improved – MadProgrammer Aug 21 '20 at 22:46