Tuesday 4 August 2015

Reading and Writing a Properties File

// Read properties file.
     Properties properties = new Properties();
    try {
        properties.load(new FileInputStream("filename.properties"));
        String s= prproperties.getProperty("Key1");
        System.out.println(s); //Value1
    } catch (IOException e) {
        e.printStackTrace();
    }

// Write properties file.
    try {
        prproperties.setProperty("Key1", "ValueXxx");
        properties.store(new FileOutputStream("filename.properties"), null);
    } catch (IOException e) {
        e.printStackTrace();
    }

//filename.properties before write
    Key1=Value1
    Key2=Value2
    Key3=Value3

//filename.properties after write

    Key1=ValueXxx
    Key2=Value2
    Key3=Value3

No comments:

Post a Comment