Java Language Copying Input Stream to Output Stream


Example

This function copies data between two streams -

void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[8192];
    while ((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
    }
}

Example -

 // reading from System.in and writing to System.out
copy(System.in, System.out);