'2012/03/29'에 해당되는 글 4건
- 2012.03.29 자바 : Input,Output의 transient
- 2012.03.29 자바 : ObjectoutputStream : 객체를 입력받은 파일에서 정보 갖고오기
- 2012.03.29 자바 : ObjectInputStream 파일 만들기
- 2012.03.29 자바 : BufferedReader와 PrintWriter
public class ObjectStreamTest {
private static String fileName = "D:\\person.obj";//매개변수 받은 객체를 파일로 출력
public static void writeobject(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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null)
{
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static Object readObject(){
//person.obj파일에 젖아된 객체 정보를 읽어들여 다시 객체로 만든다.
//ObjectInputStream - 객체를 입력받는 메소드 : 객체 역직렬화
//메소드 : readObject() : Objectream
ObjectInputStream ois = null;
Object obj = null;
try {
ois = new ObjectInputStream (new FileInputStream(fileName));
//읽기
obj = ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ois!=null)
{
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return obj;
}
public static void main(String[] args) {
//PersonDTO p = new PersonDTO("id-111","112312","홍길동",123);
//writeobject(p);//직렬화가 안되서 Exception오류가 나게 된다.
Object obj = readObject();
PersonDTO dto = (PersonDTO)obj;
System.out.println(dto);
}
}
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 ObjectStreamTest2 {
private static String fileName = "D:\\person.obj";//매개변수 받은 객체를 파일로 출력
public static void writeobject(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) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null)
{
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
PersonDTO p = new PersonDTO("id-111","112312","홍길동",123);
writeobject(p);//직렬화가 안되서 Exception오류가 나게 된다.
}
}
import java.io.Serializable;
public class PersonDTO implements Serializable{
private String id;
private String password;
private String name;
private int age;
//생성자, 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 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 + "]";
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class BufferReaderWriterTest {
public static void main(String[] args) {
//Filter Steram을 이용한 입출력
//BufferedRedaer와 PrintWriter를 이용해 news.txt->news3.txt로 입출/력
FileReader fr = null;
FileWriter fw = null;
//Filter스트림
BufferedReader br = null;//버퍼를 이용, 라인단위로 읽는 기능
PrintWriter pw = null;//데이터 포맷에 맞게 출력, println(), print()
try{
//1-1 d연결
fr = new FileReader("D:\\news.txt");
fw = new FileWriter("D:\\news4.txt");
//1-2 filter 스트림 추가
br = new BufferedReader(fr);
//fr 노드 스트림에다가 BufferedReader 필터를 추가해준다.
pw = new PrintWriter(fw);//한꺼번에 출력하는는 경우에 쓰인다.
//pw = new PrintWriter(fw,true);//true : auto flush - println()하면 바로 출력된다.
//쓸때마다 출력해야 하는 경우는(true)를 더하면 된다, 바로바로 출력하는 채팅에 쓰이게 된다.
//fw 노드 스트림에다가 PrintWriter 필터를 추가해준다,
//2. read(BufferedReader)와 writer(PrintWriter)를 해준다.
String str = br.readLine();//엔터를 기준으로 읽어들임(엔터는 안읽는다.)
while(str!=null)//EOF : null을 리턴
{
pw.println(str);
System.out.println(str);
str=br.readLine();
}
//pw.flush();//buffer에 있는 데이터를 최종 출력장소로 밀어내는 메소드
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
//3. 연결끊기 - Filter를 끊으면 된다.
if(br!=null)
{
try{
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(pw!=null){
pw.close();//throws안함, 내부적으로 처리함
}
}
}
}