title: Closing Java Input/Output streams
body: <p>This is my code:</p>
  
  <pre><code>FileOutputStream fos = null;
  DataOutputStream dos = null;
  try {
      fos = new FileOutputStream(file);
      dos = new DataOutputStream(fos);
  
      .... writing to file ....
  
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } finally {
      if (dos != null) {
          try {
              dos.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
      if (fos != null) {
          try {
              fos.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I read that you only need to close <code>DataOutpustStream</code> and it will close all other streams, is this correct? Does it do any harm if i close <code>FileOutputStream</code> anyways? What is the best way to close streams?</p>