import java.io.*;
// 객체 직렬화를 위한
// Serializable interface를 implements한 클래스
public class PersonInformation implements Serializable {
// 멤버 변수
private String name;
private int age;
private String address;
private String telephone;
// Constructor
public PersonInformation( String name, int age, String address, String telephone ) {
this.name = name;
this.age = age;
this.address = address;
this.telephone = telephone;
}
// 각 멤버 변수의 값을 리턴 시키는 getXXX()
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
public String getTelephone() {
return telephone;
}
}
+
import java.io.*;
import java.util.Date;
public class ObjectStreamTest {
// PersonInforamtion 객체를 선언 !!!
PersonInformation gemini;
PersonInformation johnharu;
Date d;
// Constructor
public ObjectStreamTest() {
// PersonInforamtion 객체를 생성
gemini = new PersonInformation( "gemini", 10, "seoul", "02-321-3234" );
johnharu = new PersonInformation( "johnharu", 20, "seoul", "02-473-4232" );
// 날짜정보를 지니는 Data객체 생성
d = new Date();
}
// File에 객체를 저장하는 메소드
public void writeObjectFile() {
try {
// 파일에 저장하기 위한 FileOutputStream생성
FileOutputStream fos = new FileOutputStream( "person.dat" ); // 얘를 초기값으로
// 파일에 객체를 저장하기 위한 ObjectOutputStream 객체 생성
// argument로 FileOutputStream 객체를 받음
ObjectOutputStream oos = new ObjectOutputStream( fos );
// 얘를 정의 했으므로 !! writeObject 사용가능
// 얘를 정의 했으므로 !! writeObject 사용가능
// write()를 이용해 객체를 file에 저장
oos.writeObject( gemini ); //writeObject !!! : 객체자체를 출력!!!
oos.writeObject( johnharu );
oos.writeObject( d );
} catch( Exception e ) {
System.out.println( e.toString() );
}
}
// File에서 객체를 읽어오는 메소드
public void readObjectFile() {
try {
// 파일에서 데이타를 읽어오기 위한 FileInputStream객체 생성
FileInputStream fis = new FileInputStream( "person.dat" );
// File에 저장된 객체를 읽어 오기 위해
// FileInputStream 객체를 생성자 argument를 받아 객체 생성
ObjectInputStream ois = new ObjectInputStream( fis );
Object o = null;
// 파일에 저장된 객체를 모두 읽어 올때까지 반복
while(( o = ois.readObject() ) != null ) {
// 만약 읽어온 객체가 PersonInforamtion 객체이면
if( o instanceof PersonInformation ) {
System.out.print(((PersonInformation)o).getName() + " : " +
((PersonInformation)o).getAge() );
System.out.println();
} else {
System.out.println( o.toString() );
//-> dateClass = 값이 없을 경우 자동적으로 날짜 출력
//-> dateClass = 값이 없을 경우 자동적으로 날짜 출력
}
}
} catch ( Exception e ) {}
}
public static void main( String[] args ) {
ObjectStreamTest ost = new ObjectStreamTest();
ost.writeObjectFile();
ost.readObjectFile();
}
}
- Open in Google Docs Viewer
- Open link in new tab
- Open link in new window
- Open link in new incognito window
- Download file
- Copy link address
- Edit PDF File on PDFescape.com
element
| Font | |
|---|---|
| font-family | |
| font-size | |
| font-style | |
| font-variant | |
| font-weight | |
| letter-spacing | |
| line-height | |
| text-decoration | |
| text-align | |
| text-indent | |
| text-transform | |
| white-space | |
| word-spacing | |
| color | |
| Background | |
| bg-attachment | |
| bg-color | |
| bg-image | |
| bg-position | |
| bg-repeat | |
| Box | |
| width | |
| height | |
| border-top | |
| border-right | |
| border-bottom | |
| border-left | |
| margin | |
| padding | |
| max-height | |
| min-height | |
| max-width | |
| min-width | |
| outline-color | |
| outline-style | |
| outline-width | |
| Positioning | |
| position | |
| top | |
| bottom | |
| right | |
| left | |
| float | |
| display | |
| clear | |
| z-index | |
| List | |
| list-style-image | |
| list-style-type | |
| list-style-position | |
| Table | |
| vertical-align | |
| border-collapse | |
| border-spacing | |
| caption-side | |
| empty-cells | |
| table-layout | |
| Effects | |
| text-shadow | |
| -webkit-box-shadow | |
| border-radius | |
| Other | |
| overflow | |
| cursor | |
| visibility |
