Save file info for processing not seen by user in Eclipse - java

I am working on a project in Eclipse wherein I need to save extra information attached with file and some derived from files such that user can't see them directly. For e.g., after loading file, some process is done on that file and I need to save result of that process such that I can use it later on.
The information that I need to store can be long strings and in form of bytes too.
Is there any feature in Eclipse using which I can do this task?
I am using Eclipse 4.5.2

You can save strings using the setPersistentProperty and getPersistentProperty methods of IFile.
You can also save any sort of data you want in the 'state location' for your plugin.
Get the state location with:
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath location = Platform.getStateLocation(bundle);
This is a directory in the workspace .metadata.

Related

Create a Json File within Class

I've written a toDoList with Intellj in Java. I used simpleJson to save and load the tasks. In my repository I have a data folder where the json file is stored. However, when I package it with Intellj into a .jar, the save/load feature does not work. I am assuming this is because there's no json file attached in the packaging. Thus, is it possible to make a new Json file in the same folder where the .jar is so my program doesn't crash? In Intellij my code still runs if I delete the data file from the project view.
Maybe using another way to store your JSON file will be better than inside a
*.jar file because *jar are archives : direct serialization will not work unless you try to unpack/package the things. Bad way to go with complicated stuff for no added value.
If I were you, I will try to store these file in the user folder of your system. To get it, you can use :
System.getProperty("user.home");

Plugin Development, where to save user data?

I am developing a plugin for Eclipse. When the user runs the application for the first time, calibration takes place. This is where user data is taken and then printed out to text files, so that when used again, the system will remember their settings.
I was wondering where is the best place to store these text files on a user's system?
There are different options with different scopes:
per installation: see the post, create a unique folder under the mentioned path and store the files there
per user: create a unique folder under the users home directory (System.getProperty("user.dir")) and store the files there
per workspace: Plugin::getStateLocation returns a path that is unique to the given plug-in for each workspace. Store your files there. Plugin is an abstract class that is implemented by the Activator or your plug-in. If you don't have such class yet, create it by specifying the Activator in the manifest editor. Alternatively you can use Platform::getStateLocation() if you have a reference to the Bundle that represents your plug-in.
A common place to store such data is the
AppData\Local\YourPluginName
directory.
You may also use the
eclipse/plugins/YourPlugin
directory.
Your plugin can store data in the workspace metadata in the plugin 'state location'
Use something like:
Bundle bundle = FrameworkUtil.getBundle(getClass());
IPath stateLoc = Platform.getStateLocation(bundle);
stateLoc will be a directory where your plugin can store whatever it likes. The location will normally be '.metadata/.plugins/your plugin id' in the workspace.

Storing configuration file of an application in OS independent path

I am writing an application (basically eclipse plugin), so there are few combo-box, drop-downs etc, which I am getting values for them dynamically via XML file. My doubt is which is the best way to store these files in a particular directory so that it can be read in both Windows as well as Linux.
Initially I tried to create a config path under {eclipse.home.location} like:
String finalPath = System.getProperty("eclipse.home.location") +"/myAppConfig";
and store all of my plugin's configuration there (not only configuration but few helper jars which I programatically read in my plugin). But there is a probabilty that Eclipse installation maybe in shared location and user may not have write access to it.
I also tried to store it in a program files directory using:
System.getenv("ProgramFiles");
But this fails under non-windows environments. So my question is can anyone shed a light on this so that I can store in some common directory where it is valid for both windows and linux?
Kindly let me know if my wordings are confusing. Or is it possible to store my config files under plugins directory and get the path like this /plugins/myConfigDir ?
Try using the getStateLocation() method in Plugin.
That will give you an IPath that points to a user specific workspace location.

Java on MacOSX: how to access files in the bundle

I have a Java app which I am packaging to a Mac Application Bundle (That folder structure that contains all of the app but looks like a single executable file to the user).
My Problem:
I am reading and writing some config files in the local folder ("."). However, on Mac this seems to be the folder in which the application bundle is located (so usually the "Applications" folder and I obviously don't want that.
My question:
How can I store a file inside that bundle? How can I programmatically retrieve the bundle name to compute the fully qualified folder?
I know I could try to go the ClassLoader way, but I'd like to avoid that (for security reasons).
Or is there simply a better way how to store application cache and config data locally?
The Mac OS X Finder treats any directory whose name ends in .app as an application; right-click to Show Package Contents. It remains an otherwise normal directory for I/O purposes. This project is an example. See this answer regarding paths relative to the application bundle.
Addendum: Is there a better way how to store application cache and config data locally?
The example cited uses java.util.prefs.Preferences, but javax.jnlp.PersistenceService is an alternative.
Ok, the basic answer / solution is: don't do it.
The reason I originally wanted to do it was to cache larger amounts of data on the local HD. Java preferences are a good choice for config data (i.e. small data amounts) but fail to handle data in the megabyte size range.
My solution:
On MacOSX (System.getProperty("os.name").contains("Mac OS X")) I simply create a folder in the user's home folder (System.getProperty("user.home")). I prefix that folder with a . to ensure it is hidden from the user. This also ensures that I have write access to the folder (which could be a problem in the .app folder depending on where the user copies it)
On Windows (System.getProperty("os.name").contains("Windows")) I create that folder in the System.getenv("APPDATA") directory (note that this env variable only exists on Windows systems.
Now I have full access to the filesystem (even without admin rights) and can store as much data as I like.

How to store files in Android's cache directory?

My goal is store temporary files in the directory returned by Context#getCacheDir().
/data/data/my.app/cache/somedir/foo.json
I presume I can just use java.io apis to write files there. But I'm not sure how to set permissions on files there because I would not be able to use Context#MODE_PRIVATE.
The filename parameter to Context#openFileOutput and Context#openFileInput cannot accept input with path delimiters. Thus they are only useful for files created in directory:
/data/data/my.app
Each application in Android runs under it's own user account by default, so files created by your app would not be accessible from any other app. Check out bullet point #3 at the top of this page:
http://developer.android.com/guide/topics/fundamentals.html
You could also verify this by running a shell in your emulator/device and looking at the output of ls -l.
But I'm not sure how to set
permissions on files there because I
would not be able to use
Context#MODE_PRIVATE.
AFAIK, they should be private by default.

Categories