Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyAndClose(InputStream in, OutputStream out) throws IOException { byte[] cache = new byte[4096]; int size; while ((size = in.read(cache)) > 0) { out.write(cache, 0, size); } in.close(); out.close(); } }