1

I am trying to read a properties file from a package within my program without having to copy it to the plugin directory. I can read from a properties file which is in the plugin directory:

private String displayProperties()
{
    Properties props = new Properties();
    InputStream is = null;

    try 
    {
        File f = new File(getDataFolder()+"/test.properties");
        is = new FileInputStream(f);
        props.load(is);
        return props.getProperty("Test1");
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }

    return null;
}

This is all good and works BUT the user is able to edit the properties file. I have done some 'googling' and what i have found is people saying its not possible to write to a properties file in the .jar file. I am not trying to write to it, I just want to read from it.

Is this possible? and if so how can I change my code to accomplish this?

(If you know of a tutorial that would be great too)

Edit: I have now solved the problem, all I had to do was to change the input stream from file to this:

is = getClass().getResourceAsStream("test.properties");

Now it works perfectly.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
NoLiver92
  • 882
  • 3
  • 15
  • 39
  • 2
    By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Nov 16 '13 at 12:49
  • Answer and accept your question instead of editing it – Darkhogg Nov 16 '13 at 13:37
  • @Darkhogg sorry, but it wouldn't let me answer it from my phone – NoLiver92 Nov 16 '13 at 13:46

0 Answers0