0

I have a program that requires the use of File objects, which are txt files that store information. The code functions fine when just executing the java files in my IDE, the problems I face happen when I pack the program into a jar and suddenly the txt files cannot be loaded.

In my project folder, I keep all data files in a data folder, which resides in the root. At the top of my program I define the txt file as a file object,

File data = new File("data/data.txt");

Which later gets passed to a different method which accepts the file object in its constructor and preforms other functions with it.

My main confusion is how to I make it so even when my program is made into a jar I can still read and write to txt files? Is that even possible or do I need to handle the data differently?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • you need to load a `Resource`; see in example https://stackoverflow.com/questions/34496802/cannot-load-resource-in-jar – Daniele Jun 17 '18 at 21:28
  • see also https://stackoverflow.com/questions/3861989/preferred-way-of-loading-resources-in-java – Daniele Jun 17 '18 at 21:29

0 Answers0