Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- insert
- 안드로이드
- Java
- ubuntu
- 두 동전
- broadcastreceiver
- 데이터전달
- Jenknis
- vscode
- git
- github
- intent
- service
- 단축키
- spring
- 프로그래머스
- 17837
- 데이터
- IntelliJ
- 큐빙
- mysql
- goland
- activity
- 백준
- 제어반전
- 알고리즘
- Android
- 16197
- Algorithm
- data
Archives
- Today
- Total
해보자
[Java ] Appending ObjectOutputStream 본문
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 out) throws 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
'Java' 카테고리의 다른 글
[Java] Data Structure | Deque (0) | 2020.09.08 |
---|---|
[Java] Java8 API의 default 메소드, static 메소드 (0) | 2020.09.07 |
[Java] 디렉토리 조회 기능 구현하기 (0) | 2020.07.22 |