Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,`.properties`文件在编辑模式下未显示任何内容_Java_Properties_Fileinputstream_Fileoutputstream_Properties File - Fatal编程技术网

在java中,`.properties`文件在编辑模式下未显示任何内容

在java中,`.properties`文件在编辑模式下未显示任何内容,java,properties,fileinputstream,fileoutputstream,properties-file,Java,Properties,Fileinputstream,Fileoutputstream,Properties File,我正在使用Properties对象和FileInputStream()和FileOutputStream()方法从java中的.Properties文件读取/写入属性 它的工作非常完美(我也能写和读),但当我在编辑器中打开.properties文件时,它什么也没有显示。如果我能够读写,那就很困惑了,为什么文件中没有显示值呢 这是完整的代码 String username = uName.getText().trim(); String pass = uPass.getText().trim();

我正在使用
Properties
对象和
FileInputStream()
FileOutputStream()
方法从java中的
.Properties
文件读取/写入属性

它的工作非常完美(我也能写和读),但当我在编辑器中打开
.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
仅更新属性集,不写入文件。您必须调用store(
OutputStream
out,
String
header)方法将文件实际写入磁盘,此时您必须“锁定”文件


查看

中的公认答案我猜您在编辑器中查看的文件不是用于保存/加载的文件。请记住,您的代码使用相对于程序当前工作目录的文件名…您是在IDE中运行程序吗?哪个?@TimothyTruckle netbeans中的yes在netbeans中“working”目录不是
src
文件夹。它将*.java文件编译到
bin
文件夹中,并将
src
文件夹中的任何其他文件复制到该文件夹中。因此@JonSkeetis是对的。我尝试使用srcOP中的文件夹调用StoreIt仍然是一样的。检查我的edditing;)您的代码正在工作,但在编辑模式下,属性文件中仍然没有任何内容。请关闭并打开它,可能是因为编辑器没有获取数据,因为它不希望其他应用程序更改文件。我认为您应该阅读有关文件系统的内容。然后您将理解为什么编辑器没有“动态”加载更改。