How to read file in a Eclipse Plugin? [message #327175] |
Sun, 13 April 2008 07:23  |
Eclipse User |
|
|
|
Originally posted by: jefftymc.yahoo.com
Hi All,
In a plugin project I want to save and load info from
"plugin.properties" file. This file is placed under the root folder.
Code is like this:
String pluginPath = null;
URL bundleRootURL = TestPlugin.getDefault().getBundle().getEntry("/");
try {
URL pluginUrl = FileLocator.resolve(bundleRootURL);
pluginPath = pluginURL.getPath();
} catch (IOException e) {
}
// get the plugin path such as
// "D:/workspace/TestPlugin/"
pluginPath = pluginPath.substring(1);
Properties prop = new Properties();
String path = pluginPath + "plugin.properties";
InputStream is = new FileInputStream(path);
prop.load(is);
In Running & Debug mode it works well. But when I export the
project to myplugin.jar and place it into Eclipse's plugin folder it
didn't work.
BTW: Loading from the file is for a PreferencePage. I download the
some example source code and found that they all use like this: (in a
PreferencePage)
IPreferenceStore ps = getPreferenceStore();
String s1 = ps.getValue(key1, value1); // for save
...
String s2 = ps.getString(key2); // for load
We can only choose IPreferenceStore for saving & loading? Cann't we
do the same with reading & writting from/into the file in the plugin
project?
Would u please help me with that? Thanks in advance!
|
|
|
|
|
|
|
|
|
Re: How to read file in a Eclipse Plugin? [message #327239 is a reply to message #327209] |
Mon, 14 April 2008 09:27   |
Eclipse User |
|
|
|
Originally posted by: jefftymc.yahoo.com
"You may store data by yourself inside a directory you
created for your plug-in or you may use the preference store"
If what I want to load are not simple key/value paris, how to do
that? i.e. I want to read a velocity template like:
<html>
<body>
hi $name,
This is the No. &number letters.
<body>
</html>
As a matter of convenience I want to packed the template into the
plugin jar file. When eclipse load the plugin how can I read from the file?
In most cases the key/value paris that loaded from IPreferenceStore
is enough, but if a special file is needed to read inside the plugin
jar, must we use like this?
IPreferenceStore ps = AbstractUIPlugin.getPreferenceStore();
ps.setValue("template", "<html><body>hi $name,This is the No.
&number letters.<body></html>");
Achim Lörke wrote:
> On Mon, 14 Apr 2008 09:39:21 +0800, jeffty <jefftymc@yahoo.com> wrote:
>
>> Errr... I c.
>> After setting in a preference page of Eclipse and restart Eclipse, I
>> find that my configuration has been saved successfully. How Eclipse do
>> that? It must write the info into a file but we don't know which one is it.
>>
>
> The preference settings are stored using IPreferenceStore, you can get
> an instance using the AbstractUIPlugin.getPreferenceStore() method.
> The data is stored in the workspace directory in a subdirectory called
> ".metadata". You may store data by yourself inside a directory you
> created for your plug-in or you may use the preference store (no need
> to use the preference dialog for this, just store your data in
> key/value pairs).
>
>
> Achim
|
|
|
Re: How to read file in a Eclipse Plugin? [message #327274 is a reply to message #327239] |
Tue, 15 April 2008 02:58   |
Eclipse User |
|
|
|
Originally posted by: jefftymc.yahoo.com
Anyway thanks to Merks and Achim, I've learnt much from you. :)
jeffty wrote:
> "You may store data by yourself inside a directory you
> created for your plug-in or you may use the preference store"
>
> If what I want to load are not simple key/value paris, how to do
> that? i.e. I want to read a velocity template like:
> <html>
> <body>
> hi $name,
> This is the No. &number letters.
> <body>
> </html>
> As a matter of convenience I want to packed the template into the
> plugin jar file. When eclipse load the plugin how can I read from the file?
> In most cases the key/value paris that loaded from IPreferenceStore
> is enough, but if a special file is needed to read inside the plugin
> jar, must we use like this?
> IPreferenceStore ps = AbstractUIPlugin.getPreferenceStore();
> ps.setValue("template", "<html><body>hi $name,This is the No.
> &number letters.<body></html>");
>
>
> Achim Lörke wrote:
>> On Mon, 14 Apr 2008 09:39:21 +0800, jeffty <jefftymc@yahoo.com> wrote:
>>
>>> Errr... I c.
>>> After setting in a preference page of Eclipse and restart Eclipse,
>>> I find that my configuration has been saved successfully. How Eclipse
>>> do that? It must write the info into a file but we don't know which
>>> one is it.
>>>
>>
>> The preference settings are stored using IPreferenceStore, you can get
>> an instance using the AbstractUIPlugin.getPreferenceStore() method.
>> The data is stored in the workspace directory in a subdirectory called
>> ".metadata". You may store data by yourself inside a directory you
>> created for your plug-in or you may use the preference store (no need
>> to use the preference dialog for this, just store your data in
>> key/value pairs).
>>
>>
>> Achim
|
|
|
Re: How to read file in a Eclipse Plugin? [message #327276 is a reply to message #327239] |
Tue, 15 April 2008 03:15   |
Eclipse User |
|
|
|
On Mon, 14 Apr 2008 21:27:43 +0800, jeffty <jefftymc@yahoo.com> wrote:
> "You may store data by yourself inside a directory you
>created for your plug-in or you may use the preference store"
>
> If what I want to load are not simple key/value paris, how to do
>that? i.e. I want to read a velocity template like:
><html>
> <body>
> hi $name,
> This is the No. &number letters.
> <body>
></html>
You may create a file in the workspace .metada directory. I'm not
familiar with the API, I believe you will find it in the IResource
surroundings, see IResource.getWorkspace(). You may also use
BundleContext.getDataFile().
> As a matter of convenience I want to packed the template into the
>plugin jar file. When eclipse load the plugin how can I read from the file?
> In most cases the key/value paris that loaded from IPreferenceStore
>is enough, but if a special file is needed to read inside the plugin
>jar, must we use like this?
To read from the plugins jar you can either use the BundleClassloader
and call one of the *Resource* methods or you may use BundleFile to
get your files.
This is from memory and some brief glances at the API so please be
carefull :-)
Achim
--
Achim Lörke
Eclipse-Stammtisch in the Braunschweig, Germany area:
http://www.bredex.de/de/career/eclipse.html
|
|
|
|
Re: How to read file in a Eclipse Plugin? [message #327347 is a reply to message #327276] |
Tue, 15 April 2008 20:46  |
Eclipse User |
|
|
|
Originally posted by: jefftymc.yahoo.com
I'll try it,Thanks a lot!
Achim Lörke wrote:
> On Mon, 14 Apr 2008 21:27:43 +0800, jeffty <jefftymc@yahoo.com> wrote:
>
>> "You may store data by yourself inside a directory you
>> created for your plug-in or you may use the preference store"
>>
>> If what I want to load are not simple key/value paris, how to do
>> that? i.e. I want to read a velocity template like:
>> <html>
>> <body>
>> hi $name,
>> This is the No. &number letters.
>> <body>
>> </html>
>
> You may create a file in the workspace .metada directory. I'm not
> familiar with the API, I believe you will find it in the IResource
> surroundings, see IResource.getWorkspace(). You may also use
> BundleContext.getDataFile().
>
>> As a matter of convenience I want to packed the template into the
>> plugin jar file. When eclipse load the plugin how can I read from the file?
>> In most cases the key/value paris that loaded from IPreferenceStore
>> is enough, but if a special file is needed to read inside the plugin
>> jar, must we use like this?
>
> To read from the plugins jar you can either use the BundleClassloader
> and call one of the *Resource* methods or you may use BundleFile to
> get your files.
>
> This is from memory and some brief glances at the API so please be
> carefull :-)
>
>
> Achim
|
|
|
Powered by
FUDForum. Page generated in 0.03603 seconds