Skip to content

Instantly share code, notes, and snippets.

@mertsimsek
Last active December 15, 2015 00:58
Show Gist options
  • Save mertsimsek/5176188 to your computer and use it in GitHub Desktop.
Save mertsimsek/5176188 to your computer and use it in GitHub Desktop.
Code snippet about saving and loading on JAVA by using serialization. In this one, i prefer to save/load an arraylist. You can customize it.
public static void savingArrayList(File saveFile , ArrayList<MyClass> myArrayList ){
try {
FileOutputStream fos = new FileOutputStream(saveFile);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(myArrayList);
out.flush();
out.close();
}catch (IOException e) {
System.out.println(e);
}
}
public static void loadingArrayList(File selectedFile){
try {
FileInputStream fis = new FileInputStream(selectedFile);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
ArrayList<MyClass> myNewArrayList = (ArrayList<MyClass>)in.readObject();
in.close();
}catch (Exception e) {System.out.println(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment