title: Save input stream in local path
body: <p>i am trying to hit URL and try to save the XML file into local path but i am not able to do.</p>
  
  <p>code i am using is here </p>
  
  <blockquote>
    <p>public class T_save {
        public static void download(String address, String localFileName) {
            OutputStream out = null;
            URLConnection conn = null;
            InputStream in = null;</p>
  
  <pre><code>    try {
         URL url = new URL("url");
      //  URL url = new URL(address);
          conn = url.openConnection();
          in = conn.getInputStream();
  </code></pre>
  </blockquote>
  
  <pre><code>        File file = new File(address+localFileName);
          FileWriter fileWriter = new FileWriter(file);
  
          BufferedReader reader = new BufferedReader(
                  new InputStreamReader(in));
          String line = null;
          while ((line = reader.readLine()) != null) {
              fileWriter.write(line);
          }
          fileWriter.flush();
          fileWriter.close();
  
      } catch (Exception exception) {
          exception.printStackTrace();
      } finally {
          try {
              if (in != null) {
                  in.close();
              }
              if (out != null) {
                  out.close();
              }
          } catch (IOException ioe) {
          }
      }
  }
  
  public static void download(String address) {
      int lastSlashIndex = address.lastIndexOf('\');
      if (lastSlashIndex &gt;= 0 &amp;&amp; lastSlashIndex &lt; address.length() - 1) {
          System.out.println(address.substring(0, lastSlashIndex+1)+"\t\t\t"+ 
  
    address.substring(lastSlashIndex + 1));
          download(address.substring(0, lastSlashIndex+1), address.substring
  
       (lastSlashIndex + 1));
  
      } else {
          System.err.println("Could not figure out local file name for "
                  + address);
      }
  }
  
  public static void main(String[] args) {
  
      download("C:\\Users\\praveen\\chaithu12.xml");}
      /*
       * for (int i = 0; i &lt; args.length; i++) { download(args[i]); }
  
       */
   public static class CustomAuthenticator extends Authenticator {
  
  
  
      // for entering password
  
      protected PasswordAuthentication getPasswordAuthentication() {
  
  
  
          // Get information about the request
  
          String prompt = getRequestingPrompt();
  
          String hostname = getRequestingHost();
  
          InetAddress ipaddr = getRequestingSite();
  
          int port = getRequestingPort();
  
  
  
          String username = "username";
  
          String password = "password";
  
  
  
          // Return the information (a data holder that is used by Authenticator)
  
          return new PasswordAuthentication(username, password.toCharArray());
  
  
  
      }
  }
       }
  </code></pre>