I’m trying to store all elements in a List in a file for later retrieval so when the program closes that data isn’t lost. Is this possible? I’ve written some code to try, but it’s not what I want. This is what I have written so far though.
import java.util.*;
import java.io.*;
public class Launch {
public static void main(String[] args) throws IOException {
int[] anArray = {5, 16, 13, 1, 72};
List<Integer> aList = new ArrayList();
for (int i = 0; i < anArray.length; i++) {
aList.add(anArray[i]);
}
File file = new File("./Storage.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int i = 0; i < aList.size(); i++) {
bw.write(aList.get(i));
}
bw.flush();
bw.close();
}
}
Suggestions?
Edit: I’m looking for the array itself to be written in the file, but this is what is writing.

I edited the bw.write line to change the int to a string before writing it.