← Back to feed
Renewal·서른의 생활코딩

Ex14) Java I/O - Serializable + ObjectOutputStream

NS
normalstory
cover image
import java.io.*;

// object serialization를 위한
// Serializable interface를 implements한 클래스
public class PersonInformation implements Serializable {
// member variables
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;
}
// 각 member variables의 값을 리턴 시키는 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 object를 declaration !!!
PersonInformation gemini;
PersonInformation johnharu;
Date d;
// Constructor
public ObjectStreamTest() {
// PersonInforamtion object를 생성
gemini = new PersonInformation( "gemini", 10, "seoul", "02-321-3234" );
johnharu = new PersonInformation( "johnharu", 20, "seoul", "02-473-4232" );
// 날짜정보를 지니는 Dataobject creation
d = new Date();
}
// File에 object를 저장하는 method
public void writeObjectFile() {
try {
// file에 저장하기 위한 FileOutputStream생성
FileOutputStream fos = new FileOutputStream( "person.dat" );  // 얘를 초기값으로
// file에 object를 저장하기 위한 ObjectOutputStream object creation
// argument로 FileOutputStream object를 받음
ObjectOutputStream oos = new ObjectOutputStream( fos );      
                        // 얘를 definition 했으므로 !! writeObject 사용possible
// write()를 이용해 object를 file에 저장
oos.writeObject( gemini );            //writeObject !!!  : object자체를 output!!!
oos.writeObject( johnharu );
oos.writeObject( d );
} catch( Exception e ) {
System.out.println( e.toString() );
}
}
// File에서 object를 읽어오는 method
public void readObjectFile() {
try {
// file에서 데이타를 읽어오기 위한 FileInputStreamobject creation
FileInputStream fis = new FileInputStream( "person.dat" );
// File에 저장된 object를 읽어 오기 위해
// FileInputStream object를 constructor argument를 받아 object creation
ObjectInputStream ois = new ObjectInputStream( fis );
Object o = null;
// file에 저장된 object를 모두 읽어 올때까지 반복
while(( o = ois.readObject() ) != null ) {
// 만약 읽어온 object가 PersonInforamtion object이면
if( o instanceof PersonInformation ) {
System.out.print(((PersonInformation)o).getName() + " : " +
((PersonInformation)o).getAge() );
System.out.println();
} else {
System.out.println( o.toString() );  
                                        //-> dateClass = 값이 없을 경우 자동적으로 날짜 output
}
}
} 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

This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.