문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top