title: Check if a file exists on a remote server having the URL
body: <p>How can I check in Java if a file exists on a remote server having the URL? If it is then download the file.</p>
  
  <p>Here is my code sample - it opens the specified URL and then creates I/O streams to copy the file specified by the URL.  But eventually it's not working as it supposed to do.</p>
  
  <pre><code>URL url = new URL(" //Here is my  URL");     
  url.openConnection();      
  InputStream reader = url.openStream();      
  FileOutputStream writer = new FileOutputStream("t");    
  byte[] buffer = new byte[153600];    
  int bytesRead = 0;    
  while ((bytesRead = reader.read(buffer)) &gt; 0)    
  {    
      writer.write(buffer, 0, bytesRead);    
      buffer = new byte[153600];    
  }    
  writer.close();    
  reader.close();  
  </code></pre>