我想写入一个属性文件。但它默默地从来没有奏效。仅从代码行为中我无法注意到它。我总是不得不打开属性文件并查看值是否发生了变化。但它从来没有。所以实际上我希望得到一个例外。问题似乎是我在打开 OutputStream 之前没有关闭 InputStream。但我从来不知道这一点。它花了我 3 天的时间,因为我希望 OutputStream 或 store 函数能给我一些反馈。看看代码。
File file = ResourceUtils.getFile("classpath:First.properties");
FileInputStream in = new FileInputStream(file);
Properties props = new Properties();
props.load(in);
System.out.println(props.getProperty("country"));
in.close(); // This I always forgot
FileOutputStream out = new FileOutputStream(file);
props.setProperty("country", "germany");
props.store(out, null);
System.out.println(props.getProperty("country"));
out.close();