title: Write to the same file using BufferWriter from multiple classes
body: <p>I want to output my result to a file. I use BufferWriter as below:</p>
  
  <pre><code>public class class1{
  ...
  void print()
       {
       System.out.println("The name "+outName()+" Tel: "+outNumber());
         try{
            PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
            printWriter.println("The name "+outName()+" Tel: "+outNumber());
          }catch (IOException e){}
         }
  
      }     
  </code></pre>
  
  <p>However I have another class and main function also having their own print functions</p>
  
  <pre><code>public class class2{
      ...
      void print()
           {
           System.out.println("The name "+outName()+" Tel: "+outNumber());
             try{
                PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
                printWriter.println("The name "+outName()+" Tel: "+outNumber());
              }catch (IOException e){}
             }
  
          }  
  
  public static void main(String[] args) throws IOException{
  
  try{
      PrintWriter printWriter=new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)));
      ...
      printWriter.println("something");
  
  printWriter.close();
      }catch(IOException e){ }
  
  }
  </code></pre>
  
  <p>The code pass the compilation, but only the output from the main function appear in the output file. How to fix it please? </p>