Skip to content

Instantly share code, notes, and snippets.

@zekroTJA
Created August 4, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zekroTJA/c2c3723990bd07b4a120eb41626b5ac7 to your computer and use it in GitHub Desktop.
Save zekroTJA/c2c3723990bd07b4a120eb41626b5ac7 to your computer and use it in GitHub Desktop.
Little snippet to save and read java initialized classes directly into a file
import java.io*;
/**
* Created by zekro on 24.05.2017 / 09:46
* DiscordBot/commands.chat
* © zekro 2017
*/
public class Serializer implements Serializable {
private final String saveFileName = "save.dat";
public class Test implements Serializable {
private String name;
private int count;
public Test(String name, int count) {
this.name = name;
this.count = count;
}
}
private void saveClass(Test inst) throws IOException {
FileOutputStream fos = new FileOutputStream(saveFileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(inst);
oos.close();
}
private Test loadClass() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream(saveFileName);
ObjectInputStream ois = new ObjectInputStream(fis);
Test out = (Test) ois.readObject();
ois.close();
return out;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment