1. BufferedOutputStream

- 버퍼기능을 강화시킨 1바이트 출력스트림처리 클래스


EX> public BufferedOutputStream(OutputStream out)
package test01.filter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test01_BufferedOutputStream
{
    public static void main(String[] args)
    {
        //public BufferedOutputStream(OutputStream out)
        BufferedOutputStream fos=null;

        try
        {
            //화면에 출력하기위한 1바이트 처리스트림객체 얻어오기
            //OutputStream out=System.out;

            //파일에 출력하기위한 1바이트 처리스트림 객체 생성하기
           OutputStream out=new FileOutputStream("test.dat");

            //버퍼의 크기가 1024byte인 출력스트림 생성하기
            //fos=new BufferedOutputStream(out,1024);
            fos=new BufferedOutputStream(out);

            //public void write(byte[] b, int off,int len) throws IOException
            byte[] b={65,66,67,68,69,70};
            //b배열0번째부터 배열을 크기만큼 파일에 출력하기
            fos.write(b,0,b.length);

            //fos.flush();
            fos.close();//스트림닫기
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }
    }
}
  
 

EX> hello.txt라는 파일에 2byte처리 스트림을 사용해서 문자열 출력하기.
package test01.filter;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintWriter;

public class Test02_PrintWriter
{
    public static void main(String[] args)
    {
        OutputStream os=System.out;

        //화면에 출력하기위한 2바이트 처리스트림객체 생성
        PrintWriter pw=new PrintWriter(os);

        pw.println("안녕하세요!");
        pw.println("반가워요~~~");

        //pw.close();        
        PrintWriter pw1=null;

        try
        {
            //파일에 데이터를 출력하기위한 2바이트 처리스트림객체 생성
            pw1=new PrintWriter("hello.txt");

            //파일에 문자열 출력하기
            pw1.println("안녕!");
            pw1.println("만나서 반가워요~~~");

            pw1.close();
            System.out.println("파일로 저장성공!");
        }catch(FileNotFoundException fe){
            System.out.println(fe.getMessage());
        }        
    }
}
 


 
2. DataOutputStream

 - 기본자료형(int,char,byte,long,double,..) 을 출력하는 기능을 갖는 스트림


EX> 파일에 기본자료형을 출력하기
package test01.filter;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test03_DataOutputStream
{
    public static void main(String[] args)
    {
        int a=100;
        char b='C';
        boolean c=10>5;
        double d=3.45;
        float f=4.5f;
        
        DataOutputStream dos=null;

        try{

            //파일에 기본자료형을 출력하기위한 스트림객체 생성
            dos=new DataOutputStream(new FileOutputStream("data.dat"));

            //int형을 파일에 저장->4바이트로저장
            dos.writeInt(a);

            //char형을 파일에 저장->2바이트로 저장
            dos.writeChar(b);

            //boolean형을 파일에 저장
            dos.writeBoolean(c);

            //double형을 파일에 저장->8바이트로저장
            dos.writeDouble(d);

            //float을 파일에 저장
            dos.writeFloat(f);

            //utf-8인코딩방식으로 문자열 저장하기
            dos.writeUTF("안녕");

            dos.close();
            System.out.println("파일로 저장성공");

        }catch(FileNotFoundException fe){
            System.out.println(fe.getMessage());
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }
    }
}
 
 



3. DataInputStream

 - 기본자료형(int,char,byte,long,double,..) 을 읽어오는 기능을 갖는 스트림

EX> 파일에서 데이터 읽어와 저장된 순서대로 읽기
package test01.filter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test04_DataInputStream
{
    public static void main(String[] args)
    {
        DataInputStream dis=null;
        int a;
        char b;
        boolean c;
        double d;
        float f;

        try{
            //파일에서 기본자료형을 읽어오기위한 스트림객체 생성
            dis=new DataInputStream(new FileInputStream("data.dat"));

            //파일에서 데이터를 읽어올때는 저장된 순서대로 읽어온다.
            //파일에서 int데이터읽어오기(4바이트읽어옴)
            a=dis.readInt();

            //파일에서 char데이터읽어오기(2바이트읽어옴)
            b=dis.readChar();

            //파일에서 boolean데이터읽어오기(1바이트읽어옴)
            c=dis.readBoolean();

            //파일에서 double데이터읽어오기(8바이트읽어옴)
            d=dis.readDouble();

            //파일에서 float데이터읽어오기(4바이트읽어옴)
            f=dis.readFloat();

            String aa=dis.readUTF();
            System.out.println("a:" + a);
            System.out.println("b:" + b);
            System.out.println("c:" + c);
            System.out.println("d:" + d);
            System.out.println("f:" + f);
            System.out.println("aa:" + aa);

            dis.close();
        }catch(FileNotFoundException fe){
            System.out.println(fe.getMessage());
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }
    }
}
 
 
 


4. ObjectOutputStream 

 - 객체를 저장(출력)할때 사용되는 1바이트 처리 스트림
 

EX> 출력스트림 객체(FileOutputStream)를 생성하여 파일에 출력하기.
package test01.filter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class Test05_ObjectOutputStream
{
    public static void main(String[] args)
    {
        ObjectOutputStream dos=null;

        try{
            //파일에 객체를 저장하기 위한 출력스트림객체 생성
            dos=new ObjectOutputStream(new FileOutputStream("test.ser"));

            //객체를 파일로 저장하기
            dos.writeObject(new String("hello"));
            dos.writeObject(new Integer(100));
            dos.writeObject(new Date());

            dos.close();
            System.out.println("객체를 파일로 저장성공!");
        }catch(FileNotFoundException fe){
            System.out.println(fe.getMessage());
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }
    }
}
 
 

EX> 파일에서 읽어와(FileInputStream) 화면에 출력하기.
package test01.filter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;

public class Test06_ObjectInputStream
{
    public static void main(String[] args)
    {
       ObjectInputStream ois=null;

        try{
            //파일에서 객체를 읽어오기위한 스트림 객체 생성
           ois=new ObjectInputStream(new FileInputStream("test.ser"));

            //파일에서 객체 읽어오기
            String str=(String)ois.readObject();
            Integer in=(Integer)ois.readObject();
            Date d=(Date)ois.readObject();

            System.out.println(str);
            System.out.println(in);
            System.out.println(d);

            ois.close();

        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }catch(ClassNotFoundException ce){
            System.out.println(ce.getMessage());
        }
    }
}
 
 
 

5. Serialization 

객체의 직렬화 -> 객체를 바이트단위로 일렬로 나열하는 것
 객체의 역직렬화 -> 직렬화된 객체를 다시 조합하는것
 
직렬화가능한 클래스를 만들려면 Serializable인터페이스를 상속받아야 한다.


EX>
package test01.filter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
 
public class Test07_ObjectOutputStream
{
    public static void main(String[] args)
    {
        Person per=new Person("홍길동","123456-4322111");

        ObjectOutputStream oos=null;

        try{
            oos=new ObjectOutputStream(new FileOutputStream("per.ser"));
            oos.writeObject(per);
            oos.close();
            System.out.println("객체저장성공!");
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }
    }
}

 
package test01.filter;
import java.io.Serializable;

//직렬화가능한 클래스 만들기==>Serializable인터페이스를 상속받는다.
//transient : 직렬화가 되지 않는다.
//static : 직렬화가 되지 않음.
public class Person implements Serializable{
    private String name;//이름
    public static final String aa="HongGilDong";
    
    //주민번호.transient가 있으므로 직렬화가 안됨
    private transient String sid;

    public Person(String name,String sid){
        this.name=name;
        this.sid=sid;
    }
    public String getName(){return name;}
    public String getSid(){return sid;}
}

per.ser파일에 저장된 객체를 읽어와 화면에 출력하세요!
package test01.filter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test08_ObjectInputStream
{
    public static void main(String[] args)
    {
        ObjectInputStream ois=null;

        try{
            ois=new ObjectInputStream(new FileInputStream("per.ser"));
            Person per=(Person)ois.readObject();
            System.out.println("이름:" + per.getName());
            System.out.println("주민번호:" + per.getSid());
            System.out.println(Person.aa);
            ois.close();
        }catch(IOException ie){
            System.out.println(ie.getMessage());
        }catch(ClassNotFoundException ce){
            System.out.println(ce.getMessage());
        }
    }
}
 
 
 

 

 

'JAVA' 카테고리의 다른 글

재귀 메소드  (0) 2014.09.12
File 클래스  (0) 2014.09.12
자바 IO - Stream ( Ⅰ )  (0) 2014.09.12
예외처리 ( Exception )  (0) 2014.09.12
Calendar 클래스  (0) 2014.09.12

+ Recent posts