*
The moose likes Swing / AWT / SWT and the fly likes How to edit text file in jar Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to edit text file in jar" Watch "How to edit text file in jar" New topic
Author

How to edit text file in jar

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi all.
I have a small swing application which edits the text file. I want to keep that text file inside the application jar file. N edit when ever required. Is this possible??? If yes, please help me a little.



Thanks & Regards
Adeeb.
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

Why do you need to keep the text file inside a jar. You can directly use it from the environment where you have your appln . Could you briefly elaborate & it would be helpful for others if you post the Code.


"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

You can certainly extract the file from the jar, but it really isn't practical to try to update the jar. To do that requires rebuilding the jar, which is going to be very inconvenient.

Let's suppose that this file is the configuration file for your application, and you want to allow the user to modify the configuration. The way to do that is to store the modified configuration in the user's directory. Then to find the configuration file you first look in the user's directory; if it isn't there then you get it from the jar.

You can find the user's directory by looking at the "user.home" system property.

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Thanks a lot for replying. The application just stores the info in text file. Suppose i want to use the application on other system. Then i should even copy the text file. N there will be not enough security. N also if user forget to copy the text file, n just take the jar n start to use on other system, he will miss tht previous info. Is there any way to achieve ma requirement.


Thanks.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10486
    
    9

If your application is a distributed application, the best way to store this information is on the server. Then you can get it and use it, irrespective of which machine the client uses.

In case of stand alone applications, ask yourself this question:
Suppose you configure your machine with a particular wallpaper. Would you expect the same wallpaper on a different machine?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

adeeb alexander wrote:N

karthik Suryanarayanan wrote:appln

adeeb alexander wrote:N ... N ... n ... n ... ma

Can you two please UseRealWords? Especially Adeeb; is it so hard to write "and" and "my"?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

adeeb alexander wrote:Thanks a lot for replying. The application just stores the info in text file. Suppose i want to use the application on other system. Then i should even copy the text file. N there will be not enough security. N also if user forget to copy the text file, n just take the jar n start to use on other system, he will miss tht previous info. Is there any way to achieve ma requirement.


N?

Anyway those are all pretty lame excuses. Especially the one about security, since the file is just as easily seen in the jar as in somebody's directory. And the one about where the user forgot to copy over the configuration? Not hard to fix that, really. Copy it over when you find out you forgot.

You could of course put something in place where when the user modifies the configuration, you do this:

(1) Create a new copy of the jar file containing the new configuration and everything else which was in the original jar.

(2) Terminate the application.

(3) Delete the old jar and rename the new jar to be the old jar.

(4) Start the application again.

Of course that would be inconvenient (as I said already), but if you really wanted to do that instead of doing it the simple and straightforward way, then yes, you could certainly do that.

It's worth mentioning that most -- almost all -- applications don't preserve the old configuration when they are reinstalled on a new system, and in a lot of cases you wouldn't want to do that.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Sorry, i am habituated with that language because i use yahoo all the time. Anyway, thank you all for your help.


Bye.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to edit text file in jar