Self exploding and rejaring jar file during execution

前端 未结 2 1182
被撕碎了的回忆
被撕碎了的回忆 2020-12-21 01:22

I am currently working on a program to make sitting charts for my teacher\'s classroom. I have put all of the data files in the jar. These are read in and put in to a table.

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 02:16

    Tomasz is right that the following is bad practice, but it is possible.

    The contents of the classpath are read into memory during bootstrapping, however the files are modifiable but their changes will not be reflected after initialisation. I would recommend putting the data into another file, separate to your class files, but if you insist on keeping them together, you could look at:

    • JarInputStream or ZipInputStream to read the contents of the JAR file
    • Get the JarEntry for the appropriate file
    • Read and modify the contents as you desire
    • JarOutputStream or ZipOutputStream to write the contents back out

    Make sure you're not reading the resource through the classpath and that it's coming from a file on disk / network.

提交回复
热议问题