본문 바로가기
BackEnd/Java

IO_BufferedInputStream & BufferedOutputStream

by pplucy 2020. 10. 31.

* 보조스트림

 : 기반스트림의 기능을 추가해주는 클래스(도와주는 역할의 스트림)

 

 

1. BufferedOutputStream

 : 내부 버퍼를 만들어 내부 버퍼의 크기만큼 데이터를 한 번에 저장한 다음 내부 버퍼로 부터 1Byte씩 데이터를 읽어온다.

 

  * 주의할점

      -  flush() 메소드

       : BufferedOutputStream은 내부버퍼에 있는 데이터를 파일로 모두 송출한다(얼마가 남았든)

         이 버퍼가 쌓인 데이터를 모두 보내고 또 보내는데 이 버퍼에 데이터 가득 많이 안 찬 상태에서는

         안보낸다. 그대로 버퍼안에 쌓이게 됨. 이럴 때 이 남은 버퍼를 모두 비워주는 역할,

         하지만 BufferedInputStream은 기존에 있는 자료를 읽어올 때 쓰기 때문에 flush가 필요없다.

 

 

 

2. BufferedInputStream

: 내부 버퍼를 만들어 내부 버퍼에 데이터를 저장한 다음, 내부 버퍼에서 운영체제로 데이터를 보낸다.(운영체제와 프로그램간 통신횟수를 줄여 속도 향상)

 

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
 
public class A_BufferedIO {
    
Scanner sc = new Scanner(System.in);
    
    
    public void bufferedWithFile() {
    
        //1. BufferedInputStream(보조스트림, 빠르게 읽어내기)
 
        //2. BufferedOutputStream(보조스트림, 빠르게 쓰기)
  
        System.out.println("읽어올 자원의 경로를 입력하세요.");
        String url = sc.nextLine();
        System.out.println("생성할 파일명을 입력하세요: ");
        String fileName = sc.nextLine();
        
        
        
        //System클래스의 currentTimeMillis()메소드: 어느 구간의 코드나 메소드 처리시에 발생하는 처리시간을 milliseconds로 반환해준다. 
        System.out.println("Buffer : " + System.currentTimeMillis());
        
        
        
        /* *try-with-resource문
            AutoClosable 인터페이스를 구현한 클래스들의 close()메소드를 자동으로 호출해준다.
            이 try() 안에서 생긴 것들은 finally를 쓸 필요없음
            try(){} catch{}이런 형태로 사용 */
    
        try (
            
            /* FileInputStream fis = new FileInputStream(url); //읽어내고 싶은 자원 입력
               BufferedInputStream bis = new BufferedInputStream(fis); //버퍼의 인자에 fis를 넣어 처리속도 향상
            
               하지만 !!!!
               FileInputStream의 변수 필요없이 바로 BufferedInputStream에 넣어줘도 됨 */
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(url));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
                
            
         )
            {
            
            
            int check = 0;
            while((check = bis.read()) != -1) {
                bos.write(check);
            }
            
    
 
            // BufferedOuputStream 사용시 flush()메소드 사용하기
        
            bos.flush();
 
            System.out.println("Buffer  : " + System.currentTimeMillis());
            
        } catch (FileNotFoundException e) {
            
            e.printStackTrace();        
            
        } catch (IOException e) { // try_with_resource문을 사용했을 때 던져지는 예외
        
            e.printStackTrace(); 
        } 
 
    }
    
}
cs

'BackEnd > Java' 카테고리의 다른 글

IO_FileWriter & FileReader  (0) 2020.11.10
IO_ ObjectInputStream & ObjectOutputStream  (0) 2020.11.01
IO_FileOutPutStream & FileInputStream  (0) 2020.10.31
IO _ File 클래스  (0) 2020.10.31
IO (입출력)  (0) 2020.10.31

댓글