Inzimam Tariq IT Inzimam Tariq IT - 17 days ago 8
Java Question

In java `.properties` file is not showing anything in edit mode

I'm using

Properties
object with
FileInputStream()
and
FileOutputStream()
method to read/write properties from/into
.properties
file in java.

Its working perfect (I'm able to write and read as well) But when I Open the
.properties
file in editor it shows nothing in it. Its confusing if I'm able to read and write then why values are not showing in that file?

Here is complete code

String username = uName.getText().trim();
String pass = uPass.getText().trim();

// Read properties file.
Properties pro = new Properties();
try {
pro.load(new FileInputStream("conf.properties"));
pro.setProperty("user", username);
pro.setProperty("pass", pass);
pro.store(new FileOutputStream("conf.properties"), null);

String user = pro.getProperty("user");
System.out.println(user);
System.out.println("successful .......");

} catch (IOException ex) {
ex.printStackTrace();
}

Answer

Files are not flushed until you close them.

You have to change your code to include the call to the method .close() on the file input stream and close the output stream too, because the method store makes a call to .flush() but not to close, so your file system will not show you the changes:

String username = uName.getText().trim();
String pass = uPass.getText().trim();

// Read properties file.
Properties pro = new Properties();
try {

    final FileInputStream fileInputStream = new FileInputStream("conf.properties");
    pro.load(new FileInputStream("conf.properties"));
    fileInputStream.close();
    pro.setProperty("user", username);
    pro.setProperty("pass", pass);
    String user = pro.getProperty("user");
    System.out.println(user);

    final FileOutputStream fileOutputStream = new FileOutputStream("conf.properties");
    pro.store(fileOutputStream, null);
    fileOutputStream.close();
    System.out.println("successful .......");

} catch (IOException ex) {
    ex.printStackTrace();
}

You only have to write the code that extract the properties in the format you want to store them. That should do the trick. (I've coded directly here, sorry if there's a mistake)

EDITING: I just coded it, it works:

public static void main(String[] args) {
    String username = "bla";
    String pass = "blabla";

    // Read properties file.
    Properties pro = new Properties();
    try {
        File file = new File("/tmp/conf.properties");
        file.createNewFile();
        final FileInputStream fileInputStream = new FileInputStream(file);
        pro.load(fileInputStream);
        fileInputStream.close();
        pro.setProperty("user", username);
        pro.setProperty("pass", pass);
        String user = pro.getProperty("user");
        System.out.println(user);

        File toClose = new File("/tmp/conf.properties");
        final FileOutputStream fileOutputStream = new FileOutputStream(toClose);
        pro.store(fileOutputStream, null);
        fileOutputStream.close();

        System.out.println("successful .......");

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

This is the output:

cat /tmp/conf.properties 
#Sun Nov 20 18:23:58 CET 2016
user=bla
pass=blabla

Maybe the problem resides in other place? try compiling, packaging it and then running it in the terminal (java -jar ...)