Search code examples
javanetbeanswindows-8properties-file

Reading/Writing to Properties Files inside the jar file


So i am getting back into writing Java after 4 years so please forgive any "rookie" mistakes.

I need to have a properties file where i can store some simple data for my application. The app data itself won't reside here but i will be storing info such as the file path to the last used data store, other settings, etc.

I managed to connect to the properties file which exists inside the same package as the class file attempting to connect to it and i can read the file but i am having trouble writing back to the file. I am pretty sure that my code works (at least it's not throwing any errors) but the change isn't reflected in the file itself after the app is run in Netbeans.

enter image description here

In the above image you can see the mainProperties.properties file in question and the class attempting to call it (prefManagement.java). So with that in mind here is my code to load the file:

Properties mainFile = new Properties();
try {

    mainFile.load(prefManagement.class.getClass().getResourceAsStream("/numberAdditionUI/mainProperties.properties"));


} catch (IOException a) {

    System.out.println("Couldn't find/load file!");

}

This works and i can check and confirm the one existing key (defaultXMLPath).

My code to add to this file is:

String confirmKey = "defaultXMLPath2";

String propKey = mainFile.getProperty(confirmKey);

if (propKey == null) {

    // Key is not present so enter the key into the properties file
    mainFile.setProperty(confirmKey, "testtest");


    try{

        FileOutputStream fos = new FileOutputStream("mainProperties.properties");
        mainFile.store(fos, "testtest3");
        fos.flush();

    }catch(FileNotFoundException e ){
        System.out.println("Couldn't find/load file3!");
    }catch(IOException b){
        System.out.println("Couldn't find/load file4!");
    }



} else {

    // Throw error saying key already exists
    System.out.println("Key " + confirmKey + " already exists.");

}

As i mentioned above, everything runs without any errors and i can play around with trying to add the existing key and it throws the expected error. But when trying to add a new key/value pair it doesn't show up in the properties file afterwords. Why?


Solution

  • Your code writes to a local file mainProperties.properties the properties.

    After you run your part of code, there you will find that a file mainProperties.properties has been created locally.

    FileOutputStream fos = new FileOutputStream("mainProperties.properties");
    

    Could order not to confuse the two files you specify the local file to another name. e.g. mainAppProp.properties .

    • Read the complete contents of the resource mainProperties.properties.
    • Write all the necessary properties to the local file mainAppProp.properties.

     FileOutputStream fos = new FileOutputStream("mainAppProp.properties");
    

    switch if file exists to your local file , if not create the file mainAppProp.properties and write all properties to it.

    • Test if file mainAppProp.properties exists locally.
    • Read the properties into a new "probs" variable.
    • Use only this file from now on.

    Under no circumstances you can write the properties back into the .jar file.

    Test it like

        [...]
        if (propKey == null) {
        // Key is not present so enter the key into the properties file
        mainFile.setProperty(confirmKey, "testtest");
    
    
        [...]
        Reader reader = null;
        try
        {
        reader = new FileReader( "mainAppProp.properties" );
        Properties prop2 = new Properties();
        prop2.load( reader );
        prop2.list( System.out );
        }
        catch ( IOException e )
        {
        e.printStackTrace();
        }
        finally
        {
        if (reader != null) {
        reader.close(); 
        }
        }
        }
        [...]
       }
    

    output : with prop2.list( System.out );

    -- listing properties --
    defaultXMLPath2=testtest

    content of the file mainAppProp.properties

    #testtest3
    #Mon Jul 14 14:33:20 BRT 2014
    defaultXMLPath2=testtest