Write to a file in java
<p>How can I modify this run method to not only print the srting in the output window, but also write to a file for example outfile.txt in project directory. Also each string should be on a separate line in the file.</p>

<p>So I have already created a file in the project directory called outfile.txt</p>

<p>A the moment the code is print fine on window but not printing in the text file</p>

<p>here is the code #</p>

<pre><code>public void run() throws IOException
{
    Scanner sc = new Scanner(System.in);
    boolean cont = true;
    while (cont) {
       System.out.println("Enter text");
       String s = sc.nextLine();
       if ("*".equals(s)) {
           cont = false;
       } else {
           String result = shorthand(s);
           System.out.println(result);
           PrintWriter pw = new PrintWriter("outfile.txt");
           pw.println(result);
      }
   }
}
</code></pre>
