So I'm writing this JFrame application that has its own document model that can be loaded and saved to a filepath. I'm wondering what good ways are there to make the application open the last saved file when it starts up.
Do I store last saved document filepath in a proprietary way or is there some facility in java that can handle this for me?
Why not use the Java Preferences API ?
That allows you to store settings/properties per user and/or per system. They'll be persisted automatically (via files in Unix/Linux, and in the registry in Windows, IIRC).
Related
I have a small java utility application which performs tasks on remote computers. This app will provide user with a dropdown/combobox where they can enter ip address or hostnames for the computers they wish to connect to. It would be nice if the users could have a history of items/hosts they had connected in the dropdown.
I thought that I can create a file inside the distributable jar and use it to maintain the history. But writing to a file inside the jar seems to be impossible? The alternate approaches would be to use text files, databases etc located outside the jar. But this is not quite I would like to do as my utility app is only one file and I would like it to be completely independent of any external files. Also its not nice to have a text file stick around your jar file or create a text file each time your app is run.
Considering this case what options can I use? Are there any apis that can help in storing or keeping history?
Why don't you store this info with an hidden file in the user home directory? Many application do the same thing.
You can get the user home directory in this way
String userhome = System.getProperty("user.home");
I'd recommend keeping some .dat file somewhere associated with the JAR. Could be in same directory, or in the user's home (as #dash1e recommends) to avoid any permissions issues. I think you'll find that's the simplest solution.
Another option would be to use a Java-based database solution which could be bundled into your JAR (see Apache Derby, et al). Note that this would create files somewhere, but you wouldn't have to worry about the file-level management, as you'd just be interacting with it as a database.
One final option, if you really insist on avoiding having to maintain your own file, would be to use the Java Preferences API which provides an OS-agnostic way of storing data on the system in some obfuscated location. This is arguably a bit of a misuse of the goal of this API, but would accomplish what you're asking for.
I have to create a java applet that needs to access static data which is around 600k in size. This data is exported from an sql database. What is the the best format for that data to be stored in (xml, json, java include file), to get fastest/easiest access to it. I am a complete java noob and this question might be stupid, but is there a way to 'compile' this data in to executable so there are no additional requests to server once the applet is loaded. Thanks in advance!
I do not know what do you mean when you mention 'java include file'.
All the rest is OK. You can use either XML or JSON. It depends on your needs and taste. Just remember that JDK has built-in tools to parse XML and does not have such tools for JSON, so you will have to add external dependency (e.g. GSON). Generally it is not a problem but sometimes code size may be important for applets that are expected to be downloaded from server to client.
The other problems with applets is that unsigned applet cannot write to client's disk. So, whatever format you choose you have to store the information somewhere. You can store it on server, but server has access to DB anyway, so why to create copy?
So, my suggestion is the following. Store data in database. Create server side component (web service) that allows your applet to access the data. Applet should store in browser cookies user id, so next time user runs the applet it enters automatically.
To access browser cookie from applet user live connect and remember that applet tag should have MAYSCRIPT attribute.
If the data is static, just copy in the source tree next to your .java files.
From there, you can access it (from a class in the same package) with:
getClass().getClassLoader().getResourceAsStream("name");
i wonder session between Web Application and Desktop Application.
In web applications we can use Session or cookies(or HTTP Session - Stateful if use EJB), but in Desktop Application how can i manage state of object? (if not use Stateful ), i usually use managed state in text file or xml file, it mean when user login , user information will save in text or xml file for managed state and when user logout, system will delete text or xml file. I need some suggest.
I think that you mix the concepts. You should not need to hold user information for desktop application, because your application has only one user instead of many users of a web application. Your application already knows who is its user..
But if you need to save user preferences about your application, as expressed at previous post , you can use The Preferences api, or just you can save to a text file.
The Preferences API is probably what you're after (I think): java.util.prefs
The java.util.pref.Preferences class, which was added in Java 1.4 is used to store and get persistent (remains on disk between program executions) hierarchical name/values pairs.These preferences are stored in an operating system dependent manner, e.g in the Windows registry or a Mac preferences file.
So by using Preferences API, same thing is going to happen file I/O etc which you are doing already but will be in a better-manageable way.
Instead, if possible you can try using Map, accessed by respective methods for insertion/retrieval of user state through a singleton class.
For Java Web Start is there a default place to store and access data related to my program? Or do I need to create a folder? For Java Web Start (assuming I don't get a program folder) is it standard to just create on in Program Files for window, Applications for mac, etc?
I would use a subdirectory in the users home directory. E.g. System.getProperty("user.home") + File.separator + ".myapp/"
But for that the user has to add extra permissions for the a web start application.
To persist you can use a properties file or XmlEncoder which are included in the JDK. Or use external libraries like XStream, Xvantage or the simple framework where it is simple as
xstream.save(anyObject)
In addition to the Preferences API for storing user settings there are a few services that can be found in the javax.jnlp package.
For your concrete requirement the PersistenceService would be particularly useful.
Alternatively you can simply provide all data that your application requires as part of your .jar files, reference them in your .jnlp file and customize how and when they are downloaded by using the DownloadService.
There is no specific default place to store and access data related to your program with webstart. However Java does have the Preferences API to provide a platform independant way of storing configuration without worrying about the specific storage location/format.
I have a Java application that runs on Mac and Windows, directly off a CD/DVD with no installation. Now, I need to store a file containing per-user data (think favourites etc.) somewhere on the local file system, so that it can be written to.
So, where do you think is a good location for this file? I am thinking something like:
<USER_DOCUMENTS_AND_SETTINGS>/application data/myapp/favourites.db for windows
<USER_HOME_DIR>/.myapp/favourites.db for mac/nix
Thoughts?
And does anyone know the best way to determine these paths within Java?
System.getProperty("user.home")
As well as user.home, the following is useful for more temporary file storage:
System.getProperty("java.io.tmpdir")
Use the Java Preferences API, designed specifically to store user preferences values in a platform independent way we all like. Java will take care of saving them and retrieving from the file or other backing store depending on the OS.
These tutorials can get you going
You can consider storing the data at Roaming directory under Windows.