public class Example {
private static final String FILE_NAME = "state.dat";
private int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static void main(String[] args) {
Example obj = new Example();
try (InputStream in = new FileInputStream(FILE_NAME)) {
obj.setValue(in.read());
} catch (IOException exc) {
exc.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try (OutputStream out = new FileOutputStream(FILE_NAME)) {
out.write(obj.getValue());
} catch (IOException exc) {
exc.printStackTrace();
}
}));
}
}