* byte계열
DataInputStream -|
|-Primitive Data를 입출력하는 기능제공.
DataOutputStream -|
int i = 1456729;
FileOutputStream fo;
fo.write(i);
* project : day27
package : file.io.filter
class : DataStreamTest
package file.io.filter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreamTest {
private static String fileName = "d:\\prim.dat";
public static void writeData(){
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(fileName));
dos.writeInt(301201001);
dos.writeBoolean(false);
dos.writeLong(200100101010100L);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void readData(){
DataInputStream dis = DataInputStream();
try {
dis = new DataInputStream(new FileInputStream(fileName));
int i = dis.readInt();
boolean b = dis.readBoolean();
long l = dis.readLong();
System.out.println(i);
System.out.println(b);
System.out.println(l);
// System.out.println(i+b+l);
System.out.println(i+"\n"+b+"\n"+l);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static DataInputStream DataInputStream() {
// TODO Auto-generated method stub
return null;
}
public static void main(String[] args) {
writeData();//binary로 생겨서 editor로 안읽힌다.
readData();//파일로부터 저장되어있는 값을 읽어 들인다.
}
}
--------------------
==========================================================================
* 예제
Human h = new Human("홍길동",20,'B',170.9,new AddressDTO("111-222","서울특별시 송파구");
DataOutStream dos = new DataOuputStream(new FileOutputStream("human.dat");
dos.writeUTF(h.getName());
dos.writeInt(h.getAge());
dos.writeUTF(h.getAddress().getZipCode());
* ObjectInputStream - 입력 -> 객체 역직렬화
|- 객체를 입출력 하기위한 기능을 제공
ObjectOutputStream - 출력 -> 객체 직렬화
- java.io - Serializable를 implements한 class의 객체들만 직렬화 할 수 있다. (크게 공통점이 없는 것들을 하나의 것으로 만들어 주는 것이 인터페이스다. 뒤에 able이 붙는다.)
직렬화
name = "홍길동" --------------------> 홍길동 10 b 180
age = 10; <--------------------
bloodType = 'b' 역직렬화
tall = 180
package : day27
* project : day27
package : file.io.filter
class : ObjectStreamTest
PersonDTO
package file.io.filter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectStreamTest {
private static String fileName = "d:\\person.obj";
public static void wrtiteObject(Object obj){
//인수로 받은 객체를 person.obj에 출력
//ObjectOutputStream 이용. 객체를 출력 - 객체 직렬화,
// 출력대상 : instance변수의 값(attribute)
ObjectOutputStream oos = null;
try {
//연결 + 필터
oos = new ObjectOutputStream(new FileOutputStream(fileName));
//쓰기 - writeObject(object)
oos.writeObject(obj);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static Object readObject(){
//Person.obj파일에 저장된 객체 정보를 읽어 들여 다시 객체로 만든다.
//ObjectInputStream - 객체를 입력받는 메소드 : 객체 역직렬화
//메소드 : readObject() ; Object
ObjectInputStream ois = null;
Object obj = null;
//연결 + 필터
try {
ois = new ObjectInputStream(new FileInputStream(fileName));
//읽기
obj = ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return obj;
}
public static void main(String[] args) {
AddressDTO address = new AddressDTO("111-222","서울시 송파구 가락동");
PersonDTO p = new PersonDTO("id-111","1111","홍길동",20,address);
wrtiteObject(p);
Object obj = readObject();
PersonDTO pto = (PersonDTO) obj;
System.out.println(pto);
}
}
-------------------
package file.io.filter;
import java.io.Serializable;
public class PersonDTO implements Serializable{
private String id;
private String password;
private String name;
private int age;
private AddressDTO address;
//생성자, setter/getter, toString
public PersonDTO() {
}
public PersonDTO(String id, String password, String name, int age) {
this.id = id;
this.password = password;
this.name = name;
this.age = age;
}
public PersonDTO(String id, String password, String name, int age,
AddressDTO address) {
this.id = id;
this.password = password;
this.name = name;
this.age = age;
this.address = address;
}
public AddressDTO getAddress() {
return address;
}
public void setAddress(AddressDTO address) {
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "PersonDTO [id=" + id + ", password=" + password + ", name="
+ name + ", age=" + age + ", address=" + address + "]";
}
}
-----------------------
package file.io.filter;
import java.io.Serializable;
public class AddressDTO implements Serializable{
private String zipcode;
private String address;
//생성자, setter/getter, toString
public AddressDTO(String zipcode, String address) {
this.zipcode = zipcode;
this.address = address;
}
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "AddressDTO [zipcode=" + zipcode + ", address=" + address + "]";
}
}
//결과 : PersonDTO [id=id-111, password=1111, name=홍길동, age=20, address=AddressDTO [zipcode=111-222, address=서울시 송파구 가락동]]
'프로그래밍 > JAVA프로그래밍' 카테고리의 다른 글
File I/O (0) | 2012.07.29 |
---|---|
예외처리(Exception), try catch finally (0) | 2012.07.29 |
난수구하기 및 수학함수 (0) | 2012.07.29 |
java 시간 날짜 처리 (0) | 2012.07.29 |
String class, String buffer (0) | 2012.07.29 |