title: How to download a file over HTTP and store its content in a String in Java
body: <p>I'm trying to download a file over HTTP and store its contents in a String, as the title says. My approach is thus:</p>
  
  <pre><code>URL u = new URL("http://url/file.txt");
  
  ByteArrayBuffer baf = new ByteArrayBuffer(32);
  InputStream in = (InputStream) u.getContent(); 
  BufferedInputStream bis = new BufferedInputStream(in);
  
  int buffer;
  while((buffer = bis.read()) != -1){
      baf.append((byte)buffer);
  }
  
  bis.close();
  in.close();
  </code></pre>
  
  <p>The code fails when it tries to read from the stream, reporting the stream is closed.</p>
  
  <p>Now if you try to access the file through a browser, it won't be served as text, rather as a file to be downloaded.</p>
  
  <p>I haven't gotten anywhere searching the web on this, so a little insight would be much appreciated!</p>
  
  <p>Thanks.</p>