Keeping history of items without using a database in java - java

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.

Related

Java, Codename One: Exporting and -most importantly- importing files between users

Here the problem: my app will generate some files, and I want to give to the users the opportunity to exchange these files between them.
This requires 3 steps:
Saving the data: easily done in Storage implementing the
functions required by the Externalizable class;
Sharing the data: done (probably, right now it's impossible to check if the
result is correct because the missing step 3) with the sharing
methods offered by the framework, as soon as I understood I needed
to use as mimetype "application/octect-stream";
importing the downloaded data (shared by another user): this one I can't manage to
find a way to make it work. Loading the files from the app's Storage
is easy, but accessing to the folders out of the app's Storage is
something I can't manage to do.
I used FileSystemStorage in the hope of gaining access at least to the Download folder that (mostly) every phone has, but apparently I can't manage to accomplish the task
Using the FileSystemStorage on Android, for example, I have access to
/storage/emulated/0
/storage/emulated/legacy
file:///system
The first two being related to the Storage of the app.
Acceding to file:/// I obtain a long list of folders, a partial list including
media
logs
sdcard
Removable
...
root
...
But when I try to access some of these, they all appear to be empty. Either I make some mistake or the app can't see their content.
So I wonder if there is a way to accomplish the task, namely to have access to the files (probably in the Download folder) the user has downloaded, to import them.
Phone apps live in isolation within the phone. Android literally creates a separate Linux user for every app so they don't step on each other and damage the phone. iOS does similar tricks.
As a result apps can't just write a file to downloads and can't just list all the files there. This would violate the users privacy. So the native API to share a file is usually separate from the files API. We do have a share API in the CN class which lets you share images etc. to an arbitrary OS app. See isNativeShareSupported and share.
Ok, maybe I found a solution for reading the files from the Download folder in an extension of CodenameOne called FileChooser.
According to this blog post it should give access to, between the others, the Download folder (at least in Android).
I'm going to try it and, when everything is ready and tested, edit this reply to say how it worked out for me.

How to load and save files in Android?

I need to load "configuration" type files for my program in Android, they are both .bin files containing dictionary data for the NLP library. I'm a bit new to Android still, and I'm having trouble finding a folder to place the files in so I can access them when the activity starts.
I also need to create/save/load a filetype specific to my program, and I don't know where to put it either. All I've been able to find on here is people using the getAssetManager() function to fetch input streams, but I explicitly need File objects for me to be able to load them into my pre-existing desktop software code I'd like to reuse (plus the libraries require them anyway)
I've also seen people using a "res/raw" folder, however the ADT did not generate this "raw" file when I made the project - so I'm not sure what to do there either.
Here is how I usually start the software in the desktop version, but I need to fetch these files in an Android environment:
brain.start(new File("memboric.core"), new File("en_pos_maxent.bin"), new File("en_sent.bin"));
core = brain.getInterpreter().getCore();
The memboric.core file can be generated, but I need to know WHERE and HOW to do so.
Thank you very much for your time, feel free to direct me to other resources if you feel this question is inadequate.
TLDR; how do I load "static" files for the software to function (needs to be included with software), and how to create/load/save "personal" files into an appropriate area of the device?
Use Context.getFilesDir(). Your application can read and write files in that folder and they'll automatically get deleted if your application gets uninstalled.
From that point forward, you can create, delete and read from files like any other Java application.
the "raw"-folder you can create it on your own. So check this out, which shows how to handle files in Android: http://developer.android.com/training/basics/data-storage/files.html

Data storage within jar

I am trying to create a java standalone program and was wondering if there is a way to have some sort of data storage within my jar?
You see, my initial idea was to use xml files within the jar for reading and writing stuff in. I had no problem reading stuff from the xml files but I was told you cant really edit/create new files within a jar.
I want it so when i send the executable jar to my friend, initial data I have put in will be in it already and then they can just add to or change the data. I find it pretty tricky as it has to be a form of data storage without the internet access or any need for my friend to install anything more.
I decided to go for sqlite in the end. It works exactly how i want it to work. I might look into javaDB in the future as it seems to have better SQL language support sqlite.org/cvstrac/wiki?p=SqliteVersusDerby
As far as I know, JAR files are read-only, which cannot be rewritten to.
My suggestion is to use MS Access database as your JAR's data keeper.
As it is portable, the only weakness is that you have to bring both of your JAR and MDB files together (or put them in 1 folder).

Common place for creating files on various platforms (non-user-specific)

I write cross-platform java app and I need some place where I can store some amount of files. These files will be used by another java application that may run under another user. This mean that I cannot use:
System.getProperty("user.home");
since I may have no permissions to read/write these files. What I need is some way to get non-user-specific folder where every app can create/read/delete files (something like "C:\ProgramData" for windows).
Is there a cross-platform way to get such folder(at least for Windows and Linux), or if there is no any - what is the best way to get such folders for Windows(should work on XP-7), Linux and Android.
Any pieces of puzzle are welcomed.
I'm not aware of such such a cross-platform folder which is additionally readable by all users. But you can:
Define a specific folder for each OS, commons-lang may help you determining the platform (see SystemUtils)
Check if the folder read/writeable for the current user during application start-up.
Using a central configuration (where the data exchange folder is defined for this installation) may also be an option, but this depends on the packaging of your project.

How to store high score inside a jar file

I am developing a small game in Java and I am shipping it as a single Jar file. I want to store the high scores/best times for that game somewhere. Instead of storing it in a separate file, I would like to store it in the application itself (inside the Jar) so that its not lost. Is this possible at all ? If so, how to do it programatically.
Java does not give you tools to modify the JARs which are currently run. If you really want to do it, you have to guess the location of the JAR by yourself (which might reside on a read-only filesystem) and modify it the same way you would modify any archive file.
Bottom line: it's a very bad idea, don't do it! See this question for a much more reasonable solution.
Nothing is impossible, but storing it in the jar file would make it very complicated. You might also end up with unwanted side effects like "Permission Denied" errors when the jar is owned by another user. Virus scanners might get nervous when they see jar files change without reason, etc....
I would look to the Preferences API for storing this kind of info.
I think it is a bad idea to try and store anything in the jar file. Another option is to have a web based service offered to the people playing with your game. The game could connect through a web service to your hosted server and then store everything centrally there. Not sure if it is exactly what you want but it's just an idea. It would also allow people to compete with each other.
Java JAR file is a ZIP-Archive, so you could possibly access it with standard ZIP-Tools and just extract one hisghscores.txt file, modify it and then pack it back again.

Categories

Resources