JTL.zheng wrote:
So - what is the actual *goal*?
I have made a jar application, it's a JFrame.
when the user close the app, I want to save the JFrame's location and
size inside a file.
so that the next time when the user run it again, it will be the
loaction and the size of last time it has been.
Now *that's* what I call a goal!
OK - yes this is a relatively common question.
and I don't want to save the data file outside the jar file, can I
save it inside the jar?
As I mentioned, that is *theoretically* possible, but
a great deal of hassle. I have never actually attempted
it myself.
How about these different strategies instead?
1) For Java 1.4+ applications, use the Preferences class.
This is 'transparent and invisible' to the end user. Lew is
fond of reminding me that Java 1.4 is about to enter its
End-Of-Life phase, so I am guessing most development is
for Java 1.4+ VMs.
2) For a Java Web Start app., look to the PersistenceService
class. This is also 'transparent and invisible' to the end user,
and has the advantage that even sandboxed JWS apps. can
use it. Here is an example.. <http://www.physci.org/jws/#ps>
3) (Is Lew looking?) For *pre* 1.4, non JWS apps., there is yet
another strategy. Use the Properties class to create the file,
then put it inside a special directory on the path of the user.home.
I say 'special' because we want to ..
a) ensure no other application overwites the file, and this
file is not likely to overwite the files of other apps. To do that,
put the file in a sub-directory that is based on the package name
of the main class. E.G.
com.ourcompany.theapp.OurApplication
..class would store preferences in..
(user.home)/com/ourcompany/theapp/properties.file
b) To 'hide' it, simply prefix the first directory with ".". So..
(user.home)/.com/ourcompany/theapp/properties.file
This will hide the directory to the casually browsing *windows*
user, but the directory will still be visible to Mac or Linux users
who actually care to browse user home (or indeed a Windows
user like myself - who does not agree that *any* files should be
invisible). Ultimately, I would not recommend even *trying* to
hide files from the Linux user, as they can get quite irritated
if developers try to 'hide' things from them.
(I trimmed the last part of your question, in the hope
I can steer you in a better direction.)
So what do you think? Do any of those strategies sound
like the way to go, for your application?
program.
Yes,J2SE 1.4 is in End-of-Life. Earlier versions are already dead. Many
organizations still prefer J2SE 1.4 to the current version, Goddess knows why.
JSE 5 and 6 are so much better. Even JSE 5 is getting hoary and venerable,
in I.T. terms.
One possibly nifty aspect of Preferences is that it might use the O.S.
registry as its backing store. (I don't think anything about the MS Windows
registry is "nifty", though. Unless you're fascinated by pointless destruction.)
much more intrusive. It's also messier, and wrong for your goal.