I'm attempting to write a method that will read a properties file using Class.getResource(), make changes to its values, and save the file.
public void saveDBConnectionValues(String user, String password, String host, int port) throws IOException, URISyntaxException
{
Properties dbProperties = new Properties();
File f = new File(this.getClass().getResource("db.properties").toURI());
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f))));
dbProperties.load(reader);
reader.close();
dbProperties.setProperty("user", user);
dbProperties.setProperty("pw", password);
dbProperties.setProperty("host", host);
dbProperties.setProperty("port", Integer.toString(port));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f)))
dbProperties.store(writer, null);
writer.close();
}
My db.properties file is read correctly, but the store method doesn't seem to be working here. Can someone explain why this doesn't work, and what I need to do to get it working?
Thanks
A resource and a file are two different things. A resource is loaded from the class loader, and is found in the classpath. It could be loaded from the file system, or from a jar, or from a socket (or from any other location, depending on the class loader). Writing to a resource doesn't make sense.
If you want to read and write from/to a file, don't use a resource. Use file streams or reader/writer.