title: How do I save the output of my java program in another file even if it contains commands like for, if etc?
body: <p>In my code I have printed some patterns using for and if statements. I would like to ask how do I save the pattern that comes in the output in another text file so that I can access it later?
  Here's my code</p>
  
  <pre><code>import java.io.*;
  import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  class eqns
  {
      double y,x,a,b,c;
      int u=0;
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      void XYZ()throws IOException
      {
          String x1=JOptionPane.showInputDialog(null,"The eqn of a line is ax + by + c=0 Enter value of a","GRAPHS  ©RaghavGirgaonkar",JOptionPane.PLAIN_MESSAGE);
  
          a=Double.parseDouble(x1);
           String y1=JOptionPane.showInputDialog(null," Enter value of b","GRAPHS  ©RaghavGirgaonkar",JOptionPane.PLAIN_MESSAGE);
  
          b=Double.parseDouble(y1);
           String z1=JOptionPane.showInputDialog(null," Enter value of c","GRAPHS  ©RaghavGirgaonkar",JOptionPane.PLAIN_MESSAGE);
  
          c=Double.parseDouble(z1);
         JOptionPane.showMessageDialog(null,"The eqn of a line is "+a+"x + "+b+"y + "+c+"=0","GRAPHS  ©RaghavGirgaonkar COMFIRM",JOptionPane.PLAIN_MESSAGE);
         System.out.println("                                                                              LINE: "+a+"X + "+b+"Y = "+c);
          System.out.println("                                                                          ---THE EQUATION OF A LINE IS ALWAYS A SINGLE DEGREE EQUATION---") ;
          for(y=20.0;y&gt;=-20.0;y--)
          {
              for(x=-46.0;x&lt;=46.0;x++)
              {
  
                  if(a*x + b*y == c)
                  {
                      u=u+1;
                      if(y==0&amp;&amp;x==0)
                          continue;
  
                      else if(x&gt;0)
                          System.out.print("۞ ("+x+","+y+")");
                      else if(x==0 || y==0)
                      {
                        System.out.print("۞");
                        continue;
                      }
                          else
                      System.out.print("۞");
                  }
                  else
                      System.out.print(" ");
                  if(y==0)
                  {
  
                          System.out.print("*");
                  }
                  else if(x==0)
                  { 
                          System.out.print("*");
                  }
                  else
                      System.out.print(" ");
                  }
              System.out.println();
          }
  
  
          if(0==(((0)*a + c)/b))
             JOptionPane.showMessageDialog(null,"This line passes through the origin ","GRAPHS  ©RaghavGirgaonkar",JOptionPane.PLAIN_MESSAGE);
          if(u==0)
             JOptionPane.showMessageDialog(null,"Unfortunately this line is not satisfied by intger co-ordinates hence this line cannot be printed ","GRAPHS  ©RaghavGirgaonkar",JOptionPane.PLAIN_MESSAGE);
             File f=new File("Output.txt");
             FileOutputStream fos=new FileOutputStream(f);
             PrintWriter pw=new PrintWriter(fos);
             pw.write(trial);
             pw.flush();
             fos.close();
             pw.close();
      }
  
      void main()throws IOException
      {
          eqns obj=new eqns();
          obj.XYZ();
      }
  }
  </code></pre>