title: How to write to a file multiple times in java?
body: <p>I am working on a program that is going to calculate alla prime numbers between 2 and 10 000 and write this to a file.</p>
  
  <p>People tell different ways on how to write to a file so I took one sugestion and tried it. 
  At first i put my file creation in a loop with ended in the program recreating the file each time, then I took the file creation out of the loop and now I get errors.</p>
  
  <p>Could someone tell me if this is the way to do it, if not, how should it be done, or how can i make this work?</p>
  
  <p>To already have a file could also work and then just add/edit/overwrite it from my program.</p>
  
  <pre><code>public static void main(String[] arg){
      int prime = 2;
  
      File file = new File("out.txt");      // how should i do this part
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);     
  
      while(prime &lt; 10000){
          boolean isPrime = true;
  
          for( int i = 2; i &lt;= sqrt(prime); i++){
              if( (prime%i) == 0 ){
                  isPrime = false;
                  break;
              }
          }
          if(isPrime){
              bw.write(prime + " ");     // and this
          }
          prime++;
      }
  }
  </code></pre>