title: Problem writing on a .txt file using user input
body: <p>I am writing a program that takes user input and displays it in a text file. I am having trouble having the input save on the file. Other similar questions have suggested to close the BufferedWriter, however I'm using a try-with-resource block which, as I understand it, should auto-close the resource. When I use fileWriter.close(); the text is saved however because it is being closed it will not be re-opened and I am given an IOException due to the stream being closed. How could I fix this issue?</p>
  <p>Main Method</p>
  <pre><code>import java.io.BufferedWriter;
  import java.io.IOException;
  import java.nio.charset.StandardCharsets;
  import java.nio.file.Files;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.Scanner;
  
  public class TextReader {
      public static void main(String[] args) {
          Path path = Paths.get(&quot;/Users/Coding/Desktop/myFile.txt&quot;).toAbsolutePath();
          try (Scanner scan = new Scanner(System.in);
               BufferedWriter fileWriter = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
  
              Reader reader = new Reader(scan, path, fileWriter);
              reader.menu();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  <p>Reader Class</p>
  <pre><code>import java.io.BufferedWriter;
  import java.io.IOException;
  import java.nio.file.Path;
  import java.util.Scanner;
  
  public class Reader {
  
      Path path;
      Scanner scan;
      BufferedWriter fileWriter;
  
      Reader(Scanner scan, Path path, BufferedWriter fileWriter) {
  
          this.scan = scan;
          this.path = path;
          this.fileWriter = fileWriter;
      }
  
      public void menu() throws IOException {
          String task;
  
          do{
              System.out.print(&quot;What would you like to do today?: &quot;);
              task = scan.nextLine();
              switch(task){
                  case &quot;1&quot;:
                      addData();
                      break;
                  case &quot;6&quot;:
                      System.out.println(&quot;Goodbye!&quot;);
                      System.exit(0);
                  menu();
              }
          }while(!task.equals(&quot;6&quot;));
  
      }
  
      void addData() throws IOException {
          boolean cont = false;
          do try {
              System.out.print(&quot;Enter Name of Player: &quot;);
              String playerName = scan.nextLine();
  
              System.out.print(&quot;Enter Number of Games Played: &quot;);
              int gamesPlayed = Integer.parseInt(scan.nextLine());
  
              System.out.print(&quot;Enter Number of Goals Made: &quot;);
              int goals = Integer.parseInt(scan.nextLine());
  
              System.out.print(&quot;Enter Number of Assists Made: &quot;);
              int assists = Integer.parseInt(scan.nextLine());
  
              System.out.print(&quot;Enter Number of Points Scored: &quot;);
              int points = Integer.parseInt(scan.nextLine());
  
              System.out.print(&quot;Enter Number of Saves Made: &quot;);
              int saves = Integer.parseInt(scan.nextLine());
  
              System.out.print(&quot;Enter Number of Shots Made: &quot;);
              int shotsOnGoal = Integer.parseInt(scan.nextLine());
  
              fileWriter.write(
                      playerName + &quot; &quot; + gamesPlayed + &quot; &quot; + goals + &quot; &quot; +
                              assists + &quot; &quot; + points + &quot; &quot; + saves + &quot; &quot; + shotsOnGoal);
          } catch(NumberFormatException e){
              System.out.println(&quot;Enter Valid Input&quot;);
              cont = true;
  //insert finally clause to close fileWriter here
          }while(cont);
  
      }
  }
  
  </code></pre>
  <p><strong>IF</strong> fileWriter is closed in a finally clause after catching the NumberFormatException as indicated in the comment of the code, the following Exception is displayed</p>
  <pre><code>java.io.IOException: Stream closed
      at java.base/java.io.BufferedWriter.ensureOpen(BufferedWriter.java:107)
      at java.base/java.io.BufferedWriter.write(BufferedWriter.java:224)
      at java.base/java.io.Writer.write(Writer.java:249)
      at Reader.addData(Reader.java:74)
      at Reader.menu(Reader.java:28)
      at TextReader.main(TextReader.java:16)
  </code></pre>