Copying a file using InputStream and OutputStream

suggest change

We can directly copy data from a source to a data sink using a loop. In this example, we are reading data from an InputStream and at the same time, writing to an OutputStream. Once we are done reading and writing, we have to close the resource.

public void copy(InputStream source, OutputStream destination) throws IOException {
  try {
      int c;
      while ((c = != -1) {
  } finally {
      if (source != null) {
      if (destination != null) {

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents