0

I am attempting to create a standalone app and the app uses a local csv file to pull data from. I was hoping to send this app to friends for a niche use but I have realized that should the csv not be included, there is no way of me finding out where they stored the data and the file path for it. I need the csv to be included in the jar file with the code still reading it.

I have tried a file reader loop and importing the file directly to the project but neither have succeeded so far. I am not looking to use OpenCSV unless it is the last option as I would have to rewrite about half the code.

TheDomo
  • 13
  • 2
  • 2
    Better to load your files/images as resources: [Netbeans](https://technojeeves.com/index.php/aliasjava1/78-loading-files-as-resources-in-java-with-netbeans) [Eclipse](https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse) (including csv files). You could have the best of both worlds: read a file provided by the user, else your own csv as resource in the jar – g00se Nov 24 '22 at 21:33

1 Answers1

3

You can include the CSV (and any other file) into the jar. A jar file is just a zip archive - you can verify that by renaming it and opening it with any archive viewer.

Now at runtime the file is read-only, and you have to access it via the classpath, not via the filesystem. Here is an example how to access a text file - and any other file also needs to be opened as stream: How to read text file from classpath in Java?

These files that you add to the jar are called resources, and as @g00se already mentioned: Here is how to do it in

Queeg
  • 7,748
  • 1
  • 16
  • 42