简体   繁体   中英

Is there a way to include a csv file within a java program?

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.

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 really read text file from classpath in Java

These files that you add to the jar are called resources, and @g00se gave links how you can tell your IDE to add them when building the jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM