The easiest way to do this is by using serialization. To store your information in a file, use a FileOutputStream
and an ObjectOutputStream
(from the java.io package), like this:
File saveFile = new File("filename.sav");
try {
FileOutputStream fileOut = new FileOutputStream(saveFile);
try (ObjectOutputStream objOut = new ObjectOutputStream(fileOut)) {
objOut.writeObject(number); // you should use lower case for variable names
objOut.writeObject(array);
}
} catch (IOException e) {
}
This will store your information in a binary file named "filename.sav". To get your integer and array from that file, use a FileInputStream
and an ObjectInputStream
and cast the retrieved objects to the desired type in the order they were stored in:
try {
FileInputStream fileIn = new FileInputStream("filename.sav");
try (ObjectInputStream objIn = new ObjectInputStream(fileIn)) {
int number = (int) objIn.readObject();
int[] array = (int[]) objIn.readObject();
}
} catch (IOException | ClassNotFoundException e) {
}
This will also work for any serializable object (an object that implements Serializable
).