Java Beans XMLEncoder encode object to xml
import java.beans.ExceptionListener; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.Serializable; import java.nio.charset.Charset; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.List; public class Main { public static void main(String[] args) { try {/*from w w w. j av a2s . c om*/ Code code = new Code("HTML", "Tag", 6.7); FileSystem fileSystem = FileSystems.getDefault(); FileOutputStream fos = new FileOutputStream("settings.xml"); XMLEncoder encoder = new XMLEncoder(fos); encoder.setExceptionListener(new ExceptionListener() { @Override public void exceptionThrown(Exception e) { System.out.println("Exception! :" + e.toString()); } }); encoder.writeObject(code); encoder.close(); fos.close(); FileInputStream fis = new FileInputStream("settings.xml"); XMLDecoder decoder = new XMLDecoder(fis); code = (Code) decoder.readObject(); decoder.close(); fis.close(); Path file = fileSystem.getPath("settings.xml"); List<String> xmlLines = Files.readAllLines(file, Charset.defaultCharset()); for (String line : xmlLines) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } class Code implements Serializable { private String name = "Unknown"; private String part = "Unknown"; private double height = Double.NaN; public Code(String name, String p, double height) { this.name = name; this.part = p; this.height = height; } @Override public String toString() { return "Name: " + this.name + ", Part: " + this.part + ", Height: " + this.height; } }