title: How to write html to a file in android
body: <p>I know this has been asked a few times here, but I'm not sure which way I should go.  This code downloads the html file okay, but I get an IOException when trying to write the html to a file.  I've tried many suggestions on sof, but none seem to work for me and I'm at a loss as it seems it should be working.</p>
  
  <pre><code>class Downloader extends AsyncTask&lt;URL, Void, Void&gt; {
      String site = getResources().getString(R.string.sched_hd_url);
      File sdCard = Environment.getExternalStorageDirectory();
      File dir = new File(sdCard.getAbsolutePath() + "/directory/");
      File file = new File(dir, "file.html");
  
      @Override
      protected Void doInBackground(URL... urls) {
          try {
              URL url = new URL(site);
              URLConnection yc = url.openConnection();
              BufferedInputStream in = new BufferedInputStream(new URL(site).openStream());
              OutputStream out = new FileOutputStream(file);
              int total = 0;
              int count;
              byte data1[] = new byte[1024];
              while ((count = in.read(data1)) != -1) {
                  out.write(data1);
                  total += count;
              }
              in.close();
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
      protected void onPostExecute(Void result) {
          progress.setVisibility(View.INVISIBLE);
          finish();
      }
  }
  </code></pre>
  
  <p>I run this code and no file appears in the directory that I specified.  The directory already exists, and I do have the permissions in my manifest.  Any suggestions would be greatly appreciated.</p>