You can also use the java.util.Properties to save and load properties as XML file
to save xml :
storeToXML(OutputStream os, String comment);
storeToXML(OutputStream os, String comment, String encoding);
to load xml :
loadFromXML(InputStream in)
here is an example :
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
File file = new File(getPath());
if (!file.exists()) {
Properties p1 = new Properties();
p1.setProperty("A", "Amir Ali");
try {
writeXML(p1);
System.out.println("xml saved to " + getPath());
} catch (IOException e) {
e.printStackTrace();
}
}else {
try {
Properties p2 = readXML();
System.out.println(p2.getProperty("A"));
} catch (InvalidPropertiesFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeXML(Properties properties) throws IOException {
if (properties != null) {
OutputStream os = new FileOutputStream(getPath());
properties.storeToXML(os, null);
}
}
public static Properties readXML() throws InvalidPropertiesFormatException, IOException {
InputStream is = new FileInputStream(getPath());
Properties p = new Properties();
p.loadFromXML(is);
return p;
}
private static String getPath() {
return System.getProperty("user.home") + File.separator + "properties.xml";
}
}