Source From Here
Question
I want to pass an OutputStream to an InputStream in memory without creating a file on the hard disk.
How-To
Below is the sample code using:
BufferedReader(InputStreamReader(PipedInputStream)) (In) <--- b="">---> (Out)PrintStream(PipedOutputStream(PipedInputStream))
Execution result:
Question
I want to pass an OutputStream to an InputStream in memory without creating a file on the hard disk.
How-To
Below is the sample code using:
BufferedReader(InputStreamReader(PipedInputStream)) (In) <--- b="">---> (Out)PrintStream(PipedOutputStream(PipedInputStream))
- package howto;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.PipedInputStream;
- import java.io.PipedOutputStream;
- import java.io.PrintStream;
- public class InOut {
- public static void main(String[] args) throws Exception{
- PipedInputStream pin = new PipedInputStream();
- PipedOutputStream pout = new PipedOutputStream(pin);
- PrintStream out = new PrintStream(pout);
- BufferedReader in = new BufferedReader(new InputStreamReader(pin));
- System.out.println("Writing to output stream...");
- out.println("Hello World!");
- out.flush();
- System.out.println("Text written: " + in.readLine());
- }
- }
沒有留言:
張貼留言