将输入流复制到输出流

此函数在两个流之间复制数据 -

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

示例 -

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