JAVA chapter18. IO 기반 입출력 및 네트워킹. 


18.5 보조 스트림


18.5.1 문자 변환 보조 스트림

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sec05.exam01_inputstreamreader;
 
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
 
public class InputStreamReaderExample {
    public static void main(String[] args) throws Exception {
        InputStream is = System.in;
        Reader reader = new InputStreamReader(is);    
        
        int readCharNo;
        char[] cbuf = new char[100];
        while ((readCharNo=reader.read(cbuf)) != -1) {
            String data = new String(cbuf, 0, readCharNo);
            System.out.println(data);
        }
        reader.close();
        is.close();
    }
}
cs




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package sec05.exam02_outputstreamwriter;
 
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
 
public class OutputStreamWriterExample {
    public static void main(String[] args) throws Exception {        
        FileOutputStream fos = new FileOutputStream("C:/Temp/file.txt");
        Writer writer = new OutputStreamWriter(fos);
        
        String data = "바이트 출력 스트림을 문자 출력 스트림으로 변환";
        writer.write(data);
        
        writer.flush();
        writer.close();
        fos.close();
        System.out.println("파일 저장이 끝났습니다.");
    }
}
cs



18.5.2 성능 향상 보조 스트림


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package sec05.exam03_bufferedinputstream;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
 
public class BufferedInputStreamExample {
    public static void main(String[] args) throws Exception {
        long start = 0;
        long end = 0;
        
        FileInputStream fis1 = new FileInputStream("C:/JavaProgramming/source/chap18/src/sec05/exam03_bufferedinputstream/forest.jpg");
        start = System.currentTimeMillis();
        while(fis1.read() != -1) {}
        end = System.currentTimeMillis();
        System.out.println("사용하지 않았을 때: " + (end-start) + "ms");
        fis1.close();
        
        FileInputStream fis2 = new FileInputStream("C:/JavaProgramming/source/chap18/src/sec05/exam03_bufferedinputstream/forest.jpg");
        BufferedInputStream bis = new BufferedInputStream(fis2); // 버퍼 스트림 생성
        start = System.currentTimeMillis();
        while(bis.read() != -1) {}
        end = System.currentTimeMillis();
        System.out.println("사용했을 때: " + (end-start) + "ms");
        bis.close();
        fis2.close();
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec05.exam04_bufferedreader;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
 
public class BufferedReaderExample {
    public static void main(String[] args) throws Exception {        
        InputStream is = System.in;
        Reader reader = new InputStreamReader(is);    
        BufferedReader br = new BufferedReader(reader); // 버퍼 스트림 생성
        
        System.out.print("입력: ");
        String lineString = br.readLine();
        
        System.out.println("출력: " + lineString);    
        
        br.close();
        reader.close();
        is.close();
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package sec05.exam05_bufferedoutputstream;
 
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class BufferedOutputStreamExample {
    public static void main(String[] args) throws Exception {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        
        int data = -1;
        long start = 0;
        long end = 0;
        
        fis = new FileInputStream("C:/JavaProgramming/source/chap18/src/sec05/exam05_bufferedoutputstream/forest.jpg");
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream("C:/Temp/forest.jpg"); // FileOutputStream 직접 사용
        start = System.currentTimeMillis();
        while((data = bis.read()) != -1) {
            fos.write(data); // 
        }
        fos.flush();
        end = System.currentTimeMillis();
        fos.close();     bis.close();  fis.close();
        System.out.println("사용하지 않았을 때: " + (end-start) + "ms");
        
        fis = new FileInputStream("C:/JavaProgramming/source/chap18/src/sec05/exam05_bufferedoutputstream/forest.jpg");
        bis = new BufferedInputStream(fis);
        fos = new FileOutputStream("C:/Temp/forest.jpg"); 
        bos = new BufferedOutputStream(fos); // BufferdOutputStream 사용
        start = System.currentTimeMillis();
        while((data = bis.read()) != -1) {
            bos.write(data); //
        }
        bos.flush();
        end = System.currentTimeMillis();
        bos.close(); fos.close();     bis.close(); fis.close();
        System.out.println("사용했을 때: " + (end-start) + "ms");
    }
}
cs



18.5.3 기본 타입 입출력 보조 스트림

// 바이트 스트림은 바이트 단위로 입출력하기 때문에 

  자바의 기본 데이터 타입인 boolean, char, short, int, long, float, double 단위로 입출력 할 수 없다.

// 그러나 DataInputStream과 DataOutputStream 보조 스트림을 연견하면 기본 데이터 타입으로 입출력이 가능하다



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package sec05.exam06_datainputstream_dataoutputstream;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
public class DataInputOutputStreamExample {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("C:/Temp/primitive.dat");
        DataOutputStream dos = new DataOutputStream(fos);
        
        dos.writeUTF("홍길동");
        dos.writeDouble(95.5);
        dos.writeInt(1);
        
        dos.writeUTF("감자바");
        dos.writeDouble(90.3);
        dos.writeInt(2);
        
        dos.flush(); dos.close(); fos.close();
        
        FileInputStream fis = new FileInputStream("C:/Temp/primitive.dat");
        DataInputStream dis = new DataInputStream(fis);
        
        for(int i=0; i<2; i++) {
            String name = dis.readUTF();
            double score = dis.readDouble();
            int order = dis.readInt();
            System.out.println(name + " : " + score + " : " + order);
        }
        
        dis.close(); fis.close();
    }
}
cs


18.5.4 프린터 보조 스트림

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package sec05.exam07_printstream;
 
import java.io.FileOutputStream;
import java.io.PrintStream;
 
public class PrintStreamExample {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("C:/Temp/file.txt");
        PrintStream ps = new PrintStream(fos);
        
        ps.println("[프린터 보조 스트림]");
        ps.print("마치 ");
        ps.println("프린터가 출력하는 것처럼 ");
        ps.println("데이터를 출력합니다.");
        
        ps.flush();
        ps.close();
        fos.close();
    }
}
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sec05.exam07_printstream;
 
import java.util.Date;
 
public class PrintfExample {
    public static void main(String[] args) {
        System.out.printf("상품의 가격:%d원\n"123);
        System.out.printf("상품의 가격:%6d원\n"123);
        System.out.printf("상품의 가격:%-6d원\n"123);
        System.out.printf("상품의 가격:%06d원\n"123);
        
        System.out.printf("반지름이 %d인 원의 넓이:%10.2f\n"10, Math.PI*10*10);
        
        System.out.printf("%6d | %-10s | %10s\n"1"홍길동""도적");
        
        Date now = new Date();
        System.out.printf("오늘은 %tY년 %tm월 %td일 입니다\n", now, now, now);
        System.out.printf("오늘은 %1$tY년 %1$tm월 %1$td일 입니다\n", now);
        System.out.printf("현재 %1$tH시 %1$tM분 %1$tS초 입니다\n", now);
    }
}
cs


18.5.5 객체 입출력 보조 스트림

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package sec05.exam08_objectinputstream_objectoutputstream;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class ObjectInputOutputStreamExample {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("C:/Temp/Object.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);        
        
        oos.writeObject(new Integer(10));
        oos.writeObject(new Double(3.14));
        oos.writeObject(new int[] { 123 });
        oos.writeObject(new String("홍길동"));
        
        oos.flush();    oos.close(); fos.close();
        
        FileInputStream fis = new FileInputStream("C:/Temp/Object.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
        
        Integer obj1 = (Integer) ois.readObject();
        Double obj2 = (Double) ois.readObject();
        int[] obj3 = (int[]) ois.readObject();
        String obj4 = (String) ois.readObject();
        
        ois.close(); fis.close();
        
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj3[0+ "," + obj3[1+ "," + obj3[2]);
        System.out.println(obj4);
    }
}
cs



Posted by 너래쟁이
: