해보자

[Java ] Appending ObjectOutputStream 본문

Java

[Java ] Appending ObjectOutputStream

안댕 2020. 6. 22. 01:12
Source

Obejct : User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package test;
 
import java.io.Serializable;
 
public class User implements Serializable{
    String id;
    String name;
    
    public User(String id, String name) {        
        this.id = id;
        this.name = name;
    }
    
    public void print() {
        System.out.println("id : " + id + ", name : " + name);
    }
}
cs

 

main : appending_fileOutputStream.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package test;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
 
class AppendableObjectOutputStream extends ObjectOutputStream {
    public AppendableObjectOutputStream(OutputStream outthrows IOException {
        // TODO Auto-generated constructor stub
        super(out);
    }
 
    @Override
    protected void writeStreamHeader() throws IOException {
        // TODO Auto-generated method stub
    }
}
 
public class appending_fileOutputStream {
    public static Boolean insert(String id, String name) {
        User user1 = new User(id, name);
        File file = new File("userInfo.txt");
        boolean append = file.exists();
        FileOutputStream fOut;
        ObjectOutputStream oOut;
        try {
            if (append) {
                fOut = new FileOutputStream(file, true);
                oOut = new AppendableObjectOutputStream(fOut);
            } else {
                fOut = new FileOutputStream(file, true);
                oOut = new ObjectOutputStream(fOut);
            }
            oOut.writeObject(user1);
            oOut.flush();
            oOut.close();
            fOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
 
    public static void open() {
        Object obj;
        String fileName = "userInfo.txt";
        try {
            FileInputStream fin = new FileInputStream(fileName);
            ObjectInputStream oIn = new ObjectInputStream(fin);
            try {
                while ((obj = oIn.readObject()) != null) {
                    User u = (User) obj;
                    u.print();
                }
            } catch (Exception e) {
            }
            fin.close();
            oIn.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.err.println("failed to read : " + e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("failed to read2 : " + e);
        }
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        insert("1","Apple");
        insert("2","Banana");
        insert("3""Coconut");
        insert("4""Donut");
        open();
    }
}
cs

 

Run

 

1번 Run

 

 

2번 Run