title: How to create a txt file with a String content and download that txt file
body: <p>I am using java and struts. I have a scenario where there is <b>'Download'</b> link in the page. After clicking on this link the control goes to the <b>Action class</b>, where I have String content which I need to write to a <b>.txt</b> file and then download that txt file.</p>
  
  <p>Eventually whenever we click on the download link, we should be able to download a txt file having content a particular string.</p>
  
  <p>I used below piece of code, </p>
  
  <pre><code>FileOutputStream outputStream = new FileOutputStream(fileNameWithDirectory);
  outputStream.write(fileContentString.getBytes());
  outputStream.close();
  ActionForward forward = new ActionForward("doc/" + filename);
  forward.setName(filename);
  forward.setRedirect(true);
  return forward;
  </code></pre>
  
  <p>Also I tried with <code>FileWriter</code> in place of <code>FileOutputStream</code> like,</p>
  
  <pre><code>FileWriter fileWriter = new FileWriter(fileNameWithDirectory);
  fileWriter.write(fileContentString);
  fileWriter.flush();
  fileWriter.close();
  </code></pre>
  
  <p>But always instead of downloading the txt file, the control opens a new window where the String content is written.
  Please suggest me, how would I able to download that <b>.txt</b> file.</p>