java `.properties` 文件在编辑模式下没有显示任何内容

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

我将 Properties 对象与 FileInputStream()FileOutputStream() 方法一起用于 read/write 属性 from/into .properties [=25] 中的文件=].

它工作完美(我也能写和读)但是当我在编辑器中打开 .properties 文件时,它没有显示任何内容。如果我能够读取和写入,那么为什么该文件中没有显示值?

这里是完整的代码

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();
    }

文件在您关闭之前不会刷新。

您必须更改代码以在文件输入流中包含对方法 .close() 的调用并关闭输出流,因为方法存储调用 .flush() 但不会关闭,因此您的文件系统不会向您显示更改:

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();
}

您只需编写代码,以您希望存储的格式提取属性。这应该够了吧。 (这里直接打码了,如有错误请见谅)

编辑: 我刚刚编码,它有效:

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();
    }
}

这是输出:

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

也许问题出在其他地方?尝试编译、打包然后 运行 在终端中 (java -jar ...)

setProperty merely updates the set of properties, it does not write the file. You have to call the store(OutputStream out, String header) method to actually write out the file to disk, and that is the time where you'll have to "lock" the file.

查看 this link

中接受的答案