피드로 돌아가기
새로워지기·서른의 생활코딩

ex14) java.io.* _Serializable + ObjectOutputStream

NS
normalstory
표지 이미지
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 사용가능
// 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 = 값이 없을 경우 자동적으로 날짜 출력
}
}
} 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
친절한 찰쓰씨
글쓴이
친절한 찰쓰씨
친절한 찰쓰씨 · 일상 UX 디자이너
기획·디자인·단상을 조용히 기록합니다.
작가 페이지에서 더 보기

이어서 읽기

새로워지기

꾸준히, 오래, 지치지 않고

Mar 31, 2026·8
새로워지기

테크 라이프 발란스

Feb 7, 2026·3
새로워지기

휴탈리티 박정렬

Feb 7, 2026·11