Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to a properties file does not work

I want to write into a *.properties file. Thats my code how I do this:

    properties = loadProperties("user.properties");//loads the properties file
    properties.setProperty(username, password);
        try {
                properties.store(new FileOutputStream("user.properties"), null);
                System.out.println("Wrote to propteries file!" + username + " " + password);

I do not get an exception, but I also do not get the output written into the file.

Here is also my file-structure:

enter image description here

I appreciate your answer!!!

UPDATE

I load my properties file with:

    InputStream in = ClassLoader.getSystemResourceAsStream(filename);

My question is, how to load it from a specific path?

Here is my "new" File Structure:

enter image description here

like image 365
maximus Avatar asked Nov 10 '12 13:11

maximus


Video Answer


1 Answers

Here is my testing code:

@Test
public void fileTest() throws FileNotFoundException, IOException {

    File file = null;

    Properties props = new Properties();

    props.setProperty("Hello", "World");

    URL url = Thread.currentThread().getContextClassLoader()
            .getResource("exceptions/user.properties");

    try {
        file = new File(url.toURI().getPath());

        assertTrue(file.exists());
    } catch (URISyntaxException e) {

        e.printStackTrace();
    }

    props.store(new FileOutputStream(file), "OMG, It werks!");

}

It does creates and rewrites a file in my target/classes/exceptions directory (in a maven/eclipse proyect) so I guess it really works, but of course that is not tested in a JAR file.

Here is the file:

#OMG, It werks!
#Sat Nov 10 08:32:44 CST 2012
Hello=World

Also, check this question: How can i save a file to the class path

So maybe what you want to do never will work.

like image 53
ElderMael Avatar answered Sep 22 '22 17:09

ElderMael