티스토리 뷰
#### 출력하기 ####
## 입력하기 ##
/* 객체 직렬화
* : 객체의 내용을 콘솔이나 파일, 네트워크 상으로 출력하기 위해 일직선으로 나열시키는 것.. 한바이트씩 읽기
* Serializable 인터페이스 반드시 사용
*/
// 객체 타입으로 파일로 저장하기!!!
import java.io.*;
class AAA implements Serializable{
int x = 100;
int y = 200;
int z = 300;
}
public class _4_1 {
public static void main(String[] args) throws IOException{
AAA ap = new AAA(); // x, y, z라는 내용을 ap 객체라는 방식으로 직접 전송하기?
FileOutputStream fos = new FileOutputStream(new File("ddd.txt"));
BufferedOutputStream bos = new BufferedOutputStream(fos, 1024);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(ap);
oos.close();
System.out.println("출력 완료");
}
}
## 입력하기 ##
// 읽어들이기
// 직렬화 대상 클래스(AAA)가 반드시 같이 존재해야한다.
import java.io.*;
public class _4_2 {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream(new File("ddd.txt"));
BufferedInputStream bis = new BufferedInputStream(fis, 1024);
ObjectInputStream ois = new ObjectInputStream(bis);
Object obj = null;
try{
obj = ois.readObject();
} catch(ClassNotFoundException e) {}
AAA ap = (AAA) obj;
System.out.println("ap.x = " + ap.x);
System.out.println("ap.y = " + ap.y);
System.out.println("ap.z = " + ap.z);
}
}
'dev > java' 카테고리의 다른 글
| JAVA AIP CSS 칼라 변경... (0) | 2009.05.01 |
|---|---|
| [17] Frame 클래스 기본.. 창 띄우기... (0) | 2008.09.30 |
| JAVA.UTIL.SCANNER로 텍스트 스캔하기 (0) | 2008.09.30 |
| [16] Scanner 클래스로 입력 (0) | 2008.09.29 |
| [16] Text(2바이트) 기반 입력 (0) | 2008.09.29 |
공지사항
