This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modifying a source file from a JAR  RSS feed

 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I need a bit of help trying to figure out an issue I'm having with my program. If I run my program as is in my IDE (before exporting it to an executable JAR) I am able load in a txt file with some values, modify that file by making a temporary copy of it, and then renaming the modified file to the name of the original or source file. However, when I create my JAR, I am only able to load in the original file. There is no way, apparently, to do a write to the source file because the JAR essentially locks down the text file I need to modify.

Now, my question is how can I simulate this copying of the source file, modification, and then renaming of the modified file to the original? Am I thinking about this all wrong? Here is my code I'm using thus far (the deleteFromFile method is where I am stuck in terms of getting this to work with my JAR):

 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you've found, you can't update resources which are in a JAR file. So the way to go is to store your modifiable text file somewhere else.

So your question now is, where should you store it? You should store it in the user's home folder, or perhaps in a subfolder of that which is specific to your application. You can find the user's home folder like this:



As for the simulating you asked for, the algorithm is like this:

1. Did you already write a modified version in the user's home folder? If so, read that. If not, use the code you already have to read the resource from the JAR.

2. Write the newly modified version into the user's home folder.
 
Anurag Verma
Ranch Hand
Posts: 170
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jars are supposed to contain files which are needed for your application to function properly, and are supposed to be readonly. Paul has suggested nice way of doing what you need. go for it.
 
Mike Stein
Ranch Hand
Posts: 33
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,

Thank you so much for the insight. I am going to try to implement the approach you suggested. Will let you know how everything works out!

Thanks again.
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clear question and relevant code, Mike. Have a cow for that!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!