Java I / O에서 "스트림"과 "버퍼"는 정확히 무엇을 의미합니까?
방금 BufferedReader.
나는 정확히 용어의 의미는 무엇인지 알고 싶어 Stream하고 Buffer?
또한이 코드 줄은 우리에게 무엇을 제공합니까?
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Java에는 입력 및 출력 (I / O)을위한 두 가지 종류의 클래스, 즉 스트림 및 리더 / 라이터가 있습니다.
스트림 ( InputStream, OutputStream이러한 확장 다) 읽기, 파일, 네트워크, 또는 어떤 다른 장치에서 바이너리 데이터를 쓰기위한 것입니다.
독자와 작가는 텍스트 (문자)를 읽고 쓰기위한 것입니다. 이진 데이터 (바이트)를 문자 인코딩을 사용하여 문자 로 변환하고 역으로 변환하는 스트림 상단의 레이어입니다 .
디스크에서 바이트 단위로 데이터를 읽는 것은 매우 비효율적입니다. 속도를 높이는 한 가지 방법은 버퍼를 사용하는 것입니다. 한 번에 한 바이트를 읽는 대신 한 번에 수천 바이트를 읽고 메모리의 버퍼에 넣습니다. 그런 다음 버퍼의 바이트를 하나씩 살펴볼 수 있습니다.
I / O에 대한 Oracle의 Java 자습서 에서 자세히 설명합니다.
제공 한 코드 줄을 살펴 봅니다.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.in입니다 InputStream. InputStreamReader에서 바이트를 읽는을 만듭니다 System.in. 그런 다음 그것을 BufferedReader.
그래서, 결국, 당신은이 BufferedReader에서 읽는 InputStreamReader에서 읽는 것을 System.in.
java.io 패키지 작업을 시작하는 모든 사람의 마음에있는 질문입니다. 귀하의 질문에 답하기 위해 InputStreamReader 및 BufferedReader는 자바 객체만을 나타내지 만 (특별한 것은 없음) 파일, 객체 등과 같은 다른 입력 / 출력에서 읽고 쓰는 것과 같은 io 작업을 위해 생성됩니다.
이제 줄을 서자
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
InputStreamReader는 바이트의 입력 스트림을 읽는 클래스이지만 각 바이트를 읽는 것은 비용이 많이 드는 작업이므로 버퍼링되도록 BufferedReader를 감싸고 있습니다 (데코레이터 패턴)
따라서 읽기를 시작하기 전이라도 bufferedReader는 레지스터에 일부 바이트 청크를 저장하고 읽기 작업을 수행 할 때 발생합니다. 콘솔 / 파일에서 읽는 것보다 훨씬 저렴한 그 위치에서 읽습니다. 그러나 InputStreamReader의 경우 디스크 액세스 작업이 발생할 때마다 읽기 작업을 수행 할 때
완충기:
데이터가 한 위치에서 다른 위치로 이동하는 동안 일시적으로 데이터를 저장 하는 데 사용되는 물리적 메모리 저장소 의 영역 입니다 . 즉, 물리적 메모리 저장이 될 것이다 RAM 대부분의 경우 (랜덤 액세스 메모리).
그러나이 질문의 맥락에서 Buffer 는 데이터를 읽고 쓰는 동안 사용됩니다. 데이터를 한 곳에서 다른 곳으로 이동하는 동안에는 사용할 필요가 없습니다.
버퍼의 예 : 시스템에 4GB의 RAM이있는 경우 시스템에서 4KB의 메모리 (RAM)를 버퍼 에 할당 할 수 있습니다 . KB-킬로바이트, GB-기가 바이트
I / O 스트림 (또는) 스트림 :
I / O 스트림 은 입력 소스 또는 출력 대상을 나타냅니다. 스트림은 디스크 파일, 장치, 기타 프로그램 및 메모리 배열을 포함하여 다양한 종류의 소스 및 대상을 나타낼 수 있습니다.
I / O는 입력 / 출력을 의미합니다.
따라서 Input Stream은 디스크 파일, 네트워크 연결 등과 같은 입력 소스가 될 수 있습니다.
그리고 출력 스트림은 디스크 파일, 네트워크 연결 등과 같은 출력 대상이 될 수 있습니다.
JAVA 공식 문서 에 따르면 Streams는 3 가지 유형입니다.
바이트 스트림 :
8 비트 바이트의 입력 및 출력을 수행합니다. 모든 바이트 스트림 클래스는 InputStream 및 OutputStream의 자손입니다 .
바이트 입력 스트림 클래스는 입력을 원시 바이트 로 가져 옵니다 . 바이트 출력 스트림 클래스는 출력을 원시 바이트 로 제공 합니다 .
InputStream- 직접 알려진 하위 클래스
AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream.
OutputStream- 직접 알려진 하위 클래스
ByteArrayOutputStream, FileOutputStream, FilterOutputStream, ObjectOutputStream, OutputStream, PipedOutputStream
문자 스트림 : 바이트 스트림 위에있는 레이어입니다. 문자 인코딩을 사용하여 바이트 (이진 데이터)를 문자로, 문자를 바이트로 변환합니다.
All character stream classes are descended from Reader and Writer.
Reader - Direct Known Subclasses
BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader
Writer - Direct Known Subclasses
BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter
Byte Streams & Character Streams use unbuffered I/O.
This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. To reduce this kind of overhead, the Java platform implements buffered I/O streams.
Buffered Streams:
Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty.
Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.
A program can convert an unbuffered stream into a buffered stream using the wrapping idiom, where the unbuffered stream object is passed to the constructor for a buffered stream class.
Example:
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
There are 4 buffered stream classes which are used to wrap unbuffered streams:
To create buffered Byte Streams use, BufferedInputStream and BufferedOutputStream classes.
To create buffered Character Streams use, BufferedReader and BufferedWriter classes.
A stream is the connection and actual information being passed between points. The buffer is a storage container which stores part or all of the streamed data and feeds this to the output device.
Of course, the point being that if the stream slows beyond the data rate required to show the data, then the output would pause. The buffer prevents this.
A Buffer is a portion in the memory that is used to store a stream of data from peripheral devices. Then from this buffer this stream of data is collected and stored in variables. A stream can be defined as a continuous flow of data.
The very term “input/output” means nothing more than moving data in and out of buffers. Just keep this in your mind all the time. Processes perform I/O by requesting of the operating system that data to be drained from a buffer (write operation) or that a buffer be filled with data (read operation).
Logical Diagram of how data moves
In simple terms, imagine when you type data in a keyboard, the data moves through a pipe(the stream) to the buffer and then from buffer to the disk(write operation). Similarly when data moves from disk to buffer and from buffer to your console then it is read operation.
You can read the links for better understanding. Hope it helps!.
What is buffer in Java
enter link description here
참고URL : https://stackoverflow.com/questions/15984789/what-exactly-does-stream-and-buffer-mean-in-java-i-o
'Programing' 카테고리의 다른 글
| 그래프 API에서 "실제"Facebook 프로필 사진 URL 가져 오기 (0) | 2020.10.19 |
|---|---|
| Ruby에서 부모의 클래스 이름을 얻는 방법 (0) | 2020.10.18 |
| Android 머티리얼 디자인 버튼-사전 롤리팝 (0) | 2020.10.18 |
| jQuery에서 양식 변경을 어떻게 처리합니까? (0) | 2020.10.18 |
| camelCase 또는 TitleCase를 분할하는 RegEx (고급) (0) | 2020.10.18 |