kps
kps

Reputation: 173

How to read contents from outputStream

How to read the contents from an OutputStream to a byte array or to an inputstream.

I have some data written to StreamingOutput and I need to read this data from another process.

Upvotes: 1

Views: 9167

Answers (2)

Suparna
Suparna

Reputation: 1172

Java IO Overview

The terms "input" and "output" can sometimes be a bit confusing. The input of one part of an application is often the output of another.

A program that needs to read data from some source needs an InputStream . A program that needs to write data to some destination needs an OutputStream .

An InputStream or Reader is linked to a source of data. An OutputStream or Writer is linked to a destination of data.

Example Source Code

package fileio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileReadWrite {

    void writeToFile() throws IOException{
        byte[] data = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
        OutputStream output = new FileOutputStream("/Users/suparna/var/testfile.txt");
        try {
            output.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            output.close();
        }
    }

    void readFromFile() throws IOException{
        InputStream inputstream = new FileInputStream("/Users/suparna/var/testfile.txt");
        int data;
        try {
            data = inputstream.read();
            while(data != -1) {
                doSomethingWithData(data);
                data = inputstream.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            inputstream.close();
        }
    }

    private void doSomethingWithData(int data) {
        System.out.println(data);
    }

    public static void main(String[] args) {
        FileReadWrite rw = new FileReadWrite();
        try {
            rw.writeToFile();
            rw.readFromFile();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 2

Kristian Ferkić
Kristian Ferkić

Reputation: 498

You write to an output-stream and read from an input stream. If you e.g. want a separate thread to write into an output-stream while another thread is reading that data from an input stream, you can use piped streams.

public static void main(String[] args) throws Exception {
    executor = Executors.newFixedThreadPool(1);
    PipedInputStream inputStream = new PipedInputStream();
    OutputStream outputStream = new PipedOutputStream(inputStream);

    executor.submit(new Runnable() {

        @Override
        public void run() {
            for (char c = 'a'; c <= 'z'; ++c) {
                try {
                    IOUtils.write("abcdefghijklmnopqrstuvwxyz".getBytes(), outputStream);
                } catch (IOException e) {
                    // error
                } finally {
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
    });

    String string = IOUtils.toString(inputStream);
    System.out.println(string);
}

Be aware that this only works if you asynchronously read/write the data.

Upvotes: 2

Related Questions