Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test implements Savable<Test>{
- public String name;
- public int count;
- public float x, y;
- public Test(){
- //Empty constructor for loading
- }
- public Test(String name, int count, float x, float y){
- this.name = name;
- this.count = count;
- this.x = x;
- this.y = y;
- }
- public void save(DataOutputStream out) throws IOException{
- out.writeUTF(name);
- out.writeInt(count);
- out.writeFloat(x);
- out.writeFloat(y);
- }
- public Test load(DataInputStream in) throws IOException{
- name = in.readUTF();
- count = in.readInt();
- x = in.readFloat();
- y = in.readFloat();
- return this;
- }
- }
- public class Main{
- public static void main(String[] args){
- try {
- DataOutputStream out = new DataOutputStream(new FileOutputStream("test.dat"));
- Test test = new Test("fred", 99, 4.5f, 75.33f);
- test.save(out);
- out.flush();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- DataInputStream in = new DataInputStream(new FileInputStream("test.dat"));
- Test loadedTest = new Test().load(in);
- in.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement