-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteObject.java
More file actions
71 lines (59 loc) · 2.17 KB
/
ReadWriteObject.java
File metadata and controls
71 lines (59 loc) · 2.17 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ObjectDemo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
*
* @author CongThanh
*/
public class ReadWriteObject {
public static void main(String[] args) {
MyStudent myStudent = new MyStudent("Nguyễn Công Thành", 22 ); // tao doi tuong myStudent
//Ghi Object vao file
try { // dat try cacth de tranh ngoai le khi tao va viet File
FileOutputStream f = new FileOutputStream("student.dat"); // tao file f tro den student.dat
ObjectOutputStream oStream = new ObjectOutputStream(f); // dung de ghi theo Object vao file f
oStream.writeObject(myStudent); // ghi MyStudent theo kieu Object vao file
oStream.close();
} catch (IOException e) {
System.out.println("Error Write file");
}
//Doc Object tu file
MyStudent ms = null;
try { // dat try cacth de tranh ngoai le khi tao va doc File
FileInputStream f = new FileInputStream("student.dat"); // tao file f tro den student.dat
ObjectInputStream inStream = new ObjectInputStream(f); // dung de doc theo Object vao file f
// dung inStream doc theo Object, ep kieu tra ve la MyStudent
ms = (MyStudent) inStream.readObject();
inStream.close();
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
} catch (IOException e) {
System.out.println("Error Read file");
}
// Xuat KQ
System.out.println("My name is " + ms.name + ". I am " + ms.age + " years old");
}
}
class MyStudent implements Serializable {
String name;
int age;
public MyStudent(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}