將輸入流複製到輸出流

此函式在兩個流之間複製資料 -

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);