1

I'm developing a program with NetBeans 8.0 and JavaFX Scene Builder 2.0 that need store some variables in a file, where admin users can modify it when needed, (like change server IP address, or a number value from a no editable textfield) and if they close and load again the program, the changes made in variables are kept. Like any settings section of a program.

I just try do it with the Properties file, but i have problems to store it in the same folder as .jar file. When the program execute the line new FileOutputStream("configuration.properties"); the file is created at root of the disk. As the folder of the file can be stored anywhere, i not know how indicate the right path.

Creating the properties file in the package of the main project and using getClass().getResourceAsStream("configuration.properties"); i can read it but then i can not write in for change values of variables.

Is there a better method to create a configuration file? Or properties file is the best option for this case?

My other question is whether it is possible to prevent access to the contents of the file or encrypt the content?

PD: I've been testing this part of the code in Linux operating system currently, but the program will be used in Windows 7 when ready.

Amandir
  • 19
  • 4

2 Answers2

0

If you use Maven, you can store your property files in your resources folder, say resources/properties/. When you need to load them, do this:

private Properties createProps(String name)
{
    Properties prop = new Properties();
    InputStream in = null;
    try
    {
        in = getClass().getResourceAsStream(name);
        prop.load(in);
    }
    catch (IOException ex)
    {
        System.err.println("failed to load \"" + name + "\": " + ex);
    }
    finally
    {
        try
        {
            if (in != null)
            {
                in.close();
            }
        }
        catch (IOException ex)
        {
            System.err.println("failed to close InputStream for \"" + name + "\":\n" + FXUtils.extractStackTrace(ex));
        }
    }
    return prop;
}

Where name is the full path to your properties file within your resources folder. For example, if you store props.properties in resources/properties/, then you would pass in properties/props.properties.

I am not 100% sure if you can carry over this exact procedure to a non-Maven project. You'd need to instruct whatever compiler tool you are using to also include your property files.

As far as your final question goes, in regards to encrypting your properties, I would consider posting that as a separate question (after having done thorough research to try to discover an existing solution that works for you).

CAG Gonzo
  • 589
  • 1
  • 5
  • 13
  • Thanxs for your answer. I don't use Maven but this code works well when i put the properties file in the package of the program (src folder). The problem is when i want to edit variables when program is running and keep changes for next program run, because `OutputStream` can't use `getClass().getResourceAsStream(name)` that only works for `InputStream`. – Amandir Aug 21 '14 at 07:32
0

At last i found how obtain the absolute path from folder where is .jar file to create properties file in, and read/write it. Here is the code:

File file = new File(System.getProperty("java.class.path"));
File filePath = file.getAbsoluteFile().getParentFile();    
String strPath = filePath.toString();

File testFile = new File(strPath+"/configuration.properties");

Tested in Ubuntu 13.04 And Windows 7 and it works.

For encrypt the properties values i found this thread that answer how do it.

Community
  • 1
  • 1
Amandir
  • 19
  • 4
  • Have you answered your question, then, or do you still require answers? I neglected to include the code I use for saving my properties to their files but can add that to my answer if necessary. – CAG Gonzo Aug 21 '14 at 14:06
  • There are issues with this approach if the class path contains more than one entry: see [How to get the path of a running jar file](http://stackoverflow.com/questions/15359702/java-get-location-of-jar-file) for a discussion and alternative (in the accepted answer to that question). Also consider if using alternate system properties is appropriate (e.g. `user.dir`: User working directory or `user.home`: User home directory). – jewelsea Aug 21 '14 at 18:50
  • If I can modify the properties of the file located in the jar, I would like to know how. So far I have only managed to make it out of the .jar file. My reason that want put the properties file in same path that the .jar file is because i want that when a user administrator make changes to the properties files, can share the file to other users in distinct PC for update it.If I can do the same but with the jar file, would be better. I know that can not be the best option, but at moment is the best thing I have done to date. Although I have little experience with programming in java :/ – Amandir Aug 22 '14 at 08:39