Saturday, October 29, 2016

Write to a config file

//Writing values to the config file
File configFile = new File("config.properties");
 
try {
    Properties props = new Properties();
    props.setProperty("host""www.codejava.net");
    FileWriter writer = new FileWriter(configFile);
    props.store(writer, "host settings");
    writer.close();
catch (FileNotFoundException ex) {
    // file does not exist
catch (IOException ex) {
    // I/O error
}






//Reading values from the config file
File configFile = new File("config.properties");
 
try {
    FileReader reader = new FileReader(configFile);
    Properties props = new Properties();
    props.load(reader);
 
    String host = props.getProperty("host");
 
    System.out.print("Host name is: " + host);
    reader.close();
catch (FileNotFoundException ex) {
    // file does not exist
catch (IOException ex) {
    // I/O error
}