Created
August 4, 2017 08:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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