그만큼 Java의 PipedWriter 클래스 파이프를 통해 데이터를 전달하여 두 스레드가 서로 통신할 수 있도록 합니다. 이 클래스는 프로그램의 한 부분이 데이터를 메모리에 저장하지 않고 다른 부분으로 보내도록 하려는 경우에 유용합니다.
PipedWriter 클래스의 기능:
- 파이프에 데이터를 쓸 수 있습니다.
- PipedReader로 데이터를 보내기 전에 버퍼를 사용하여 데이터를 임시로 저장합니다.
- PipedReader와 함께 작동하여 스레드 간 안전한 데이터 전송을 보장합니다.
- 파이프가 끊어지면 오류가 발생합니다.
Java에서 PipedWriter 선언
PipedWriter 클래스의 선언은 다음과 같습니다.
공개 클래스 PipedWriter는 Writer를 확장합니다.
구현된 모든 인터페이스:
- 닫을 수 있음: 이 인터페이스는 리소스를 처리하는 클래스에서 사용됩니다.
- 플러시 가능: 이 인터페이스는 데이터를 대상으로 플러시하는 데 사용됩니다.
- 추가 가능: 이 인터페이스는 기존 스트림에 데이터를 추가하는 데 사용됩니다.
- 자동 닫기 가능: 이 인터페이스를 사용하면 리소스를 자동으로 닫을 수 있습니다.
PipedWriter 클래스의 생성자
이 클래스는 두 개의 생성자로 구성되어 있으며 이를 통해 이 클래스의 객체를 다양한 방법으로 만들 수 있습니다. 이 클래스에서 사용할 수 있는 생성자는 다음과 같습니다.
1. 파이프라이터(): 이 생성자는 아직 아무 것도 연결되지 않은 PipedWriter를 만드는 데 사용됩니다.
통사론:
파이프라이터()
예:
Java// Demonstrating the working // of PipedWriter() import java.io.*; class Geeks { public static void main(String[] args) { // Create PipedWriter and PipedReader PipedWriter w = new PipedWriter(); PipedReader r = new PipedReader(); try { // Connect the PipedWriter to the PipedReader w.connect(r); // Create a thread to write data into the pipe Thread writerThread = new Thread(new Runnable() { public void run() { try { w.write('Hello from PipedWriter!'); w.close(); } catch (IOException e) { e.printStackTrace(); } } }); // Create a thread to read data from the pipe Thread readerThread = new Thread(new Runnable() { public void run() { try { int data; while ((data = r.read()) != -1) { System.out.print((char) data); } r.close(); } catch (IOException e) { e.printStackTrace(); } } }); // Start both threads writerThread.start(); readerThread.start(); // Wait for both threads to finish writerThread.join(); readerThread.join(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }
산출
Hello from PipedWriter!
안녕하세요, 자바와 함께하는 세상
2. PipedWriter(PipedReader 인스트림): PipedWriter를 생성하고 이를 PipedReader에 연결하는 데 사용된 이 생성자입니다.
통사론:
PipedWriter(PipedReader snk)
예:
Java// Demonstrating the working // PipedWriter(PipedReader snk) import java.io.*; public class Geeks { public static void main(String[] args) { try { // Create a PipedReader and a PipedWriter PipedReader r = new PipedReader(); PipedWriter w = new PipedWriter(r); // Create a thread to write data to the PipedWriter Thread writerThread = new Thread(() -> { try { w.write('Hello PipedWriter'); w.close(); } catch (IOException e) { e.printStackTrace(); } }); // Create a thread to read data from the PipedReader Thread readerThread = new Thread(() -> { try { int data; while ((data = r.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } }); // Start both threads writerThread.start(); readerThread.start(); // Wait for both threads to finish writerThread.join(); readerThread.join(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }
산출
Hello PipedWriter
Java PipedWriter 메소드
아래 이미지는 PipedWriter 클래스의 메서드를 보여줍니다.

이제 각 방법에 대해 하나씩 자세히 설명하겠습니다.
1. 쓰기(int char): 이 메서드는 파이프에 단일 문자를 쓰는 데 사용됩니다.
통사론:
공개 무효 쓰기(int char)
- 매개변수: 이 메소드는 쓰여질 문자를 나타내는 ischar라는 하나의 매개변수를 사용합니다.
- 예외: 이 메서드는 I/O 작업에 문제가 있는 경우 IOException을 반환합니다.
2. write(char[] carray int offset int maxlen): 이 메서드는 배열의 문자를 파이프에 쓰는 데 사용됩니다.
자바 하위 문자열 함수
통사론:
공공 무효 쓰기(char[] carray int offset int maxlen)
- 매개변수: 이 방법에는 아래 나열된 세 가지 매개변수가 포함됩니다.
- 캐리: 데이터를 담고 있는 문자 배열입니다.
- 오프셋: 양식 작성을 시작할 배열의 위치입니다.
- 최대 길이: 쓸 수 있는 최대 문자 수입니다.
- 예외: 이 메서드는 I/O 작업에 문제가 있는 경우 IOException을 반환합니다.
이 메서드는 PipedWriter를 PipedReader에 연결하는 데 사용됩니다.
통사론:
공개 무효 연결(PipedReader 대상)
- 매개변수: 이 메서드는 PipedWriter가 데이터 전송을 위해 연결할 PipedReader라는 하나의 매개변수 대상을 사용합니다.
- 예외: 이 메서드는 연결하는 동안 오류가 발생하면 IOException을 발생시킵니다.
4. 플러시(): 이 메서드는 파이프의 데이터를 플러시하는 데 사용됩니다.
통사론:
공공 무효 플러시()
- 매개변수: 이 메서드는 매개변수를 사용하지 않습니다.
- 예외: 이 메서드는 데이터를 플러시하는 동안 오류가 발생하면 IOException을 발생시킵니다.
5. 닫기(): 이 메서드는 PipedWriter를 닫는 데 사용됩니다.
구문:
공개 무효 닫기()
- 매개변수: 이 메서드는 매개변수를 사용하지 않습니다.
- 예외: 기록기를 닫는 데 문제가 있는 경우 이 메서드는 IOException을 발생시킵니다.
이제 PipedWriter 클래스를 사용하여 데이터를 쓰고 연결된 PipedReader를 통해 읽는 방법에 대해 논의하겠습니다.
예:
자바 배열 동적Java
// Demonstrating how to use PipedReader // and PipedWriter to transferr an array // of characters between two threads import java.io.*; public class Geeks { public static void main(String[] args) throws IOException { PipedReader r = new PipedReader(); PipedWriter w = new PipedWriter(); r.connect(w); // Must connect before use // Writing a char array char[] c = {'J' 'A' 'V' 'A'}; w.write(c 0 4); // Reading blocks if no data is written yet System.out.print('Output from the pipe:'); for (int i = 0; i < 4; i++) { System.out.print(' ' + (char) r.read()); } w.close(); r.close(); } }
산출
Output from the pipe: J A V A
PipedWriter 클래스 메소드의 작동을 보여주는 Java 프로그램
이제 일부 문자를 작성하여 출력을 플러시하고 작성기를 닫습니다.
예:
Java// Java program illustrating the working of PipedWriter // write() connect // close() flush() import java.io.*; public class Geeks { public static void main(String[] args) throws IOException { PipedReader r = new PipedReader(); PipedWriter w = new PipedWriter(); try { // Use of connect(): Connecting the writer to the reader r.connect(w); // Use of write(int byte): Writing characters to the pipe w.write(71); w.write(69); w.write(69); w.write(75); w.write(83); // Use of flush() method: Flushing the output to // make sure all data is written w.flush(); System.out.println('Output after flush():'); // Reading from the pipe for (int i = 0; i < 5; i++) { System.out.print(' ' + (char) r.read()); } // Use of close() method: Closing the writer System.out.println('nClosing the Writer stream'); w.close(); } catch (IOException e) { e.printStackTrace(); } } }
산출
Output after flush(): G E E K S Closing the Writer stream퀴즈 만들기