05 3일째...

프로그래밍/Java 2007. 11. 27. 13:29 Posted by galad

★ BufferedOutputStream / BufferedInputStream 클래스

이 두개의 클래스는 FilterOutputStream, FilterInputStream 클래스로부터 상속된 클래스이다


① BufferedOutputStream 클래스
버퍼를 사용하여 스트림을 출력
생성자
  BufferedOutputStream(OutputStream outputStream)
  BufferedOutputStream(OutputStream outputStream, int bufSize)


② BufferedInputStream 클래스
버퍼를 사용하여 스트림을 입력
생성자
  BufferedInputStream(InputStream inputStream)
  BufferedInputStream(InputStream inputStream, int bufSize)

통신의 효율을 높이기 위해 버퍼링(Buffering)을 사용



/******************************************************************************
*   파일      : BufferedStreamTest.java
*   용도      : BufferedStreamTest
*   작성자   : 성홍제
*   작성일   : 2006. 08. 02
*   Version : 1.0
******************************************************************************/

package FileIO;

import java.io.*;

public class  BufferedStreamTest
{
    // Fields
    // Constructor
    // Method
    // Main Method
    public static void main(String [] args) throws Exception
    {
        if(args.length < 2)
        {
            System.out.println("원본 파일과 복사될 파일의 이름을 입력하십시요!");
            System.exit(-1);
        }
       
        // 화면 버퍼 테스트
        System.out.write(55);   // 화면에 바로 출력하는 게 아니라 화면 출력용 버퍼에 55,

                                        // 즉 "7"을 출력한다.
                                        // 근데 이것만으로는 화면 출력용 버퍼가 충분이 차지 않으므로

                                        // 화면에 내용이 출력되지 않는다.
        System.out.flush();      // 그래서 flush()를 이용해서 버퍼의 내용을 강제적으로 출력한다.
                                        // close()를 할 때로 출력하지만, 여기서는 아직 화면 출력을

                                        // 다 끝낸 것이 아니므로 flush()를 사용.
       
        FileInputStream fin = new FileInputStream(args[0]);
        FileOutputStream fout = new FileOutputStream(args[1]);
       
        BufferedInputStream in = new BufferedInputStream(fin);
        BufferedOutputStream out = new BufferedOutputStream(fout);
       
        //BufferedInputStream in = new BufferedInputStream(new FileInputStream(args[0]));
        //BufferedOutputStream out

        //      = new BufferedOutputStream(new FileOutputStream(args[1]));
       
        int s1=0;
        byte[] buf = new byte[1024];
       
        //while((s1 = in.read()) != -1)
        while((s1 = in.read(buf)) != -1)
        {
            //out.write(buf, 0, s1);    // 파일출력
            System.out.write(buf, 0, s1);   // 화면출력           
            //System.out.write(s1);   // 화면출력 - 하나씩 할 때
        }
       
        fin.close();
        fout.close();
       
        in.close();
        out.close();
       
        System.out.println("");
        System.out.println("Copied...Successfully!");
    }
}



★ DataOutputStream / DataInputStream 클래스

이 클래스들은 자바의 기본 자료형 데이터(Primitive Type)를 바이트 스트림으로 입출력하는 기능을 제공
① DataOutputStream
FilterOutputStream 클래스로부터 상속되었고, DataOutput 인터페이스를 사용한 클래스
기본 자료형 데이터를 바이트 스트림으로 출력
생성자
  DataOutputStream(OutputStream outputStream)
② DataInputStream
FilterInputStream 클래스로부터 상속되었고, DataInput 인터페이스를 사용한 클래스
바이트 스트림으로부터 자바의 기본 자료형 데이터를 입력
생성자
  DataInputStream(InputStream inputStream)

★ DataOutput 인터페이스에 선언된 메소드
void write(int i) throws IOException   i를 스트림으로 출력
void write(byte buffer[]) throws IOException     buffer를 스트림으로 출력
void write(byte buffer[], int index, int size)  buffer의 index위치부터 size 만큼의
          throws IOException                        바이트를 스트림으로 출력
void writeBoolean(boolean b)    b를 스트림으로 출력
          throws IOException
void writeByte(int i) throws IOException       i의 하위 8비트를 스트림으로 출력
void writeBytes(String s) throws IOException     문자열 s를 스트림으로 출력
void writeChar(int i) throws IOException        i의 하위 16비트를 스트림으로 출력
void writeChars(String s) throws IOException     문자열 s를 스트림으로 출력
void writeDouble(double d) throws IOException   d를 스트림으로 출력
void writeFloat(float f) throws IOException        f를 스트림으로 출력
void writeInt(int i) throws IOException           i를 스트림으로 출력
void writeLong(long l) throws IOException    l을 스트림으로 출력
void writeShort(short s) throws IOException  s를 스트림으로 출력
void writeUTF(String s) throws IOException    s를 스트림으로 출력. 유니코드를
                                                               UTF-8 엔코딩을 이용하여 변환
★ DataInput 인터페이스에 선언된 메소드
boolean readBoolean(boolean b)   스트림으로부터 읽은 boolean을 반환
         throws IOException 
byte readByte() throws IOException     스트림으로부터 읽은 byte를 반환
char readChar() throws IOException     스트림으로부터 읽은 char를 반환
double readDouble() throws IOExceptio   스트림으로부터 읽은 double을 반환
float readFloat() throws IOException     스트림으로부터 읽은 float를 반환
long readLong() throws IOException     스트림으로부터 읽은 long을 반환
short readShort() throws IOException    스트림으로부터 읽은 short를 반환
int readInt() throws IOException        스트림으로부터 읽은 int를 반환
void readFully(byte buffer[])    스트림으로부터 buffer 크기 만큼의 바이트를

      throws IOException                읽어 buffer 배열에 저장
void readfully(byte buffer[],int index, int size) 스트림으로부터 size 만큼의 바이트를 읽어

         throws IOException                             buffer의 index 위치에 저장
int readUnsignedByte() throws IOException  스트림으로부터 읽은unsigned byte를 반환
int readUnsignedShort() throws IOException 스트림으로부터 읽은unsigned short를 반환
int skipBytes(int n)    입력 스트림으로부터 n 바이트를 스킵



/******************************************************************************
*   파일      : DataIOApp.java
*   용도      : DataIOApp
*   작성자   : 성홍제
*   작성일   : 2006. 08. 01
*   Version : 1.0
******************************************************************************/

package FileIO;

import java.io.*;

public class DataIOApp
{
    public static void main(String args[]) throws IOException
    {
        File file = new File("d:\\test.txt");
        FileOutputStream outFile = new FileOutputStream(file);
        DataOutputStream outStream = new DataOutputStream(outFile);
       
        outStream.writeBoolean(true);   // 파일에 기본형을 출력
        outStream.writeInt(12345);
        outStream.writeChar('K');
        outStream.writeDouble(3.14);
       
        System.out.println(outStream.size()+" bytes were written");
           
        outStream.close();
        outFile.close();
       
        FileInputStream inFile = new FileInputStream(file);
        DataInputStream inStream = new DataInputStream(inFile);
       
        System.out.println(inStream.readBoolean()); // 위에서 만든 파일로부터 입력 받음
        System.out.println(inStream.readInt());
        System.out.println(inStream.readChar());
        System.out.println(inStream.readDouble());
       
        inStream.close();
        inFile.close();
       
        try
        {
            Thread.sleep(6000);
        }
        catch(Exception e)
        {}
       
        file.delete();
    }
}



'프로그래밍 > Java' 카테고리의 다른 글

05 문자 단위 입출력  (0) 2007.11.27
04 ObjectOutputStream / ObjectInputStream 클래스  (0) 2007.11.27
04 입출력  (0) 2007.11.27
03 임시파일  (0) 2007.11.27
02 파일 클래스  (0) 2007.11.27