title: download from URL to file using Java (BufferedInputStream, FileOutputStream, Commons IO library, NIO)
body: <p>I have tried all the ways above to download from URL to file in java.
  All the ways work, they really downloading the files to the correct directory. There is one issue:
  The file content always in gibberish.</p>
  <p>File content example:</p>
  <p>&quot;ãˇ¨ΩKìÏ»é&amp;∂ü_˜l¥πUñëôÁ’ªnõ±QõFY˜’h°ë¡@A&quot;Èx¸ëå∂˛Ô2w2y™n¡£Zã{Îdf&lt;Hß;¯·ﬂ˛”ß◊W—„Ãë¸Ùˇˆ…cdÈ?˝√”_?y:2ù¬ß¯˛ﬂˇÎ˙‰)pG“8Ñ#:G1oÅß˜Áœ›Àó?~’Ã(
  8£èIÑÁ?~π‡ë{«–°$x{£u˘ûÑ•áÉz:íáÚgp8“¡k˝€;‰∞@óˆüüå[”E¡´N!Ø≤:h∞åw∏éÏˆt∑$ÃµWÇâ∞”SÄÁ˝◊?~[ O¬qåpDﬂ&quot;4‡òfÙX˚ÖCÍÇ&quot;</p>
  <p>Is anyone has any idea why the file dowloaded in that way? Maybe I should  try UTF-8 or something else?</p>
  <p>My code:</p>
  <pre><code>    String dirName = &quot;/Users/idanazulay/Desktop/Hotels-river/&quot;;
      String fileUrl = null;
      try {
          fileUrl = getHotelReview(&quot;en&quot;).getBody().getData().getURL();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (ParseException e) {
          e.printStackTrace();
      }
      // downloadFileFromURLUsingNIO(dirName +&quot;\\feed_en2.json&quot;, fileUrl);
      BufferedInputStream in = null;
      FileOutputStream fout = null;
      URL httpUrl = new URL(UriUtils.encodePath(fileUrl, &quot;UTF-8&quot;));
      try {
          try {
              in = new BufferedInputStream(httpUrl.openStream());
          } catch (IOException e) {
              e.printStackTrace();
          }
          try {
              fout = new FileOutputStream(dirName + &quot;\\feed_en2.json&quot;);
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          }
          byte data[] = new byte[1024];
          int count;
          try {
              while ((count = in.read(data, 0, 1024)) != -1) {
                  fout.write(data, 0, count);
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      } finally {
          if (in != null)
              in.close();
          if (fout != null)
              fout.close();
      }
      System.out.println(&quot;completed&quot;);
      return &quot;Download completed&quot;;
  
  }
  
  // Using Commons IO library
  public static void saveFileFromUrlWithCommonsIO(String fileName, String fileUrl)
          throws MalformedURLException, IOException {
      URL httpUrl = new URL(UriUtils.encodePath(fileUrl, &quot;UTF-8&quot;));
      FileUtils.copyURLToFile(httpUrl, new File(fileName));
  }
  private static void downloadFileFromURLUsingNIO(String fileName,String fileUrl) throws IOException {
        URL url = new URL(fileUrl);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fOutStream = new FileOutputStream(fileName);
        fOutStream.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fOutStream.close();
        rbc.close();
       }
       
       
  </code></pre>