title: Write data to .txt file in JAVA?
body: <p>I was wondering if it is in JAVA to write the calculated data to a text file.  My JAVA code is a GUI based gpa calculator.  I just want to add a JButton &amp; ActionListener that will write the Class Name, GPA Points, and calculated GPA to a .txt file.</p>
  
  <p>Here is my JFrame Driver Code:</p>
  
  <pre><code>import javax.swing.JFrame;
  
  public class Driver00
  
  {
  
    public static void main(String[] args)
  
    {
      /*
       * Create a frame (outside box) and write what text
       * will be displayed as the frame title
       */
      JFrame frame = new JFrame("PHILIP MCQUITTY");
  
      // give frame a size
      frame.setSize(520, 375);
  
      // set location on the computer screen will frame appear
      frame.setLocation(400, 166);
  
      // use this so when you press X in corner, frame will close
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      // Add your panel to the frame. name must match Panel class name
      frame.setContentPane(new GpaCalc());
  
      // frame.setResizable(false);
  
      // always include
      frame.setVisible(true);
    }
  }
  </code></pre>
  
  <p>Here is my JPanel Code:</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class GpaCalc extends JPanel {
    private JLabel GPALabel, c1, c2, c3, c4, c5, c6, c7, g1, g2, g3, g4, g5, g6, g7;
    private JTextField Class1, Class2, Class3, Class4, Class5, Class6, Class7, Grade1, Grade2, Grade3, Grade4, Grade5, Grade6, Grade7;
    private double GPA1, GPA2, GPA3, GPA4, GPA5, GPA6, GPA7, GPA, BigDec;
  
    public GpaCalc() {
      setLayout(new FlowLayout());
  
      // Class Labels
      GPALabel = new JLabel("0.00000000000000");
      GPALabel.setFont(new Font("Times New Roman", Font.BOLD, 60));
      GPALabel.setForeground(Color.red);
  
      c1 = new JLabel("Block 1");
      c1.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c1.setForeground(Color.black);
  
      c2 = new JLabel("Block 2");
      c2.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c2.setForeground(Color.black);
  
      c3 = new JLabel("Block 3");
      c3.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c3.setForeground(Color.black);
  
      c4 = new JLabel("Block 4");
      c4.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c4.setForeground(Color.black);
  
      c5 = new JLabel("Block 5");
      c5.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c5.setForeground(Color.black);
  
      c6 = new JLabel("Block 6");
      c6.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c6.setForeground(Color.black);
  
      c7 = new JLabel("Block 7");
      c7.setFont(new Font("Times New Roman", Font.BOLD, 20));
      c7.setForeground(Color.black);
  
      // Grade Labels
      g1 = new JLabel("GPA Points");
      g1.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g1.setForeground(Color.black);
  
      g2 = new JLabel("GPA Points");
      g2.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g2.setForeground(Color.black);
  
      g3 = new JLabel("GPA Points");
      g3.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g3.setForeground(Color.black);
  
      g4 = new JLabel("GPA Points");
      g4.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g4.setForeground(Color.black);
  
      g5 = new JLabel("GPA Points");
      g5.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g5.setForeground(Color.black);
  
      g6 = new JLabel("GPA Points");
      g6.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g6.setForeground(Color.black);
  
      g7 = new JLabel("GPA Points");
      g7.setFont(new Font("Times New Roman", Font.BOLD, 20));
      g7.setForeground(Color.black);
  
      // Class Textfields
      Class1 = new JTextField("Enter Class Name", 10);
      Class1.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class2 = new JTextField("Enter Class Name", 10);
      Class2.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class3 = new JTextField("Enter Class Name", 10);
      Class3.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class4 = new JTextField("Enter Class Name", 10);
      Class4.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class5 = new JTextField("Enter Class Name", 10);
      Class5.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class6 = new JTextField("Enter Class Name", 10);
      Class6.setHorizontalAlignment(SwingConstants.CENTER);
  
      Class7 = new JTextField("Enter Class Name", 10);
      Class7.setHorizontalAlignment(SwingConstants.CENTER);
  
      // Grade Textfields
      Grade1 = new JTextField("0.0", 10);
      Grade1.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade2 = new JTextField("0.0", 10);
      Grade2.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade3 = new JTextField("0.0", 10);
      Grade3.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade4 = new JTextField("0.0", 10);
      Grade4.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade5 = new JTextField("0.0", 10);
      Grade5.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade6 = new JTextField("0.0", 10);
      Grade6.setHorizontalAlignment(SwingConstants.CENTER);
  
      Grade7 = new JTextField("0.0", 10);
      Grade7.setHorizontalAlignment(SwingConstants.CENTER);
  
      // Button(s)
      JButton Calculate = new JButton("Calculate");
      Calculate.addActionListener(new Listener());
  
      JButton Reset = new JButton("Reset Fields");
      Reset.addActionListener(new Listener2());
  
      // Add(s)
      add(GPALabel);
      add(c1);
      add(Class1);
      add(Grade1);
      add(g1);
      add(c2);
      add(Class2);
      add(Grade2);
      add(g2);
      add(c3);
      add(Class3);
      add(Grade3);
      add(g3);
      add(c4);
      add(Class4);
      add(Grade4);
      add(g4);
      add(c5);
      add(Class5);
      add(Grade5);
      add(g5);
      add(c6);
      add(Class6);
      add(Grade6);
      add(g6);
      add(c7);
      add(Class7);
      add(Grade7);
      add(g7);
      add(Calculate);
      add(Reset);
    }
  
    // Action Listener(s)
    private class Listener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
  
        GPA1 = Double.parseDouble(Grade1.getText());
        GPA2 = Double.parseDouble(Grade2.getText());
        GPA3 = Double.parseDouble(Grade3.getText());
        GPA4 = Double.parseDouble(Grade4.getText());
        GPA5 = Double.parseDouble(Grade5.getText());
        GPA6 = Double.parseDouble(Grade6.getText());
        GPA7 = Double.parseDouble(Grade7.getText());
  
        GPA = (GPA1 + GPA2 + GPA3 + GPA4 + GPA5 + GPA6 + GPA7) / 7;
  
        GPALabel.setText("" + GPA);
  
      }
  
    }
  
    private class Listener2 implements ActionListener {
      public void actionPerformed(ActionEvent e) {
  
        Class1.setText("Enter Class Name");
        Class2.setText("Enter Class Name");
        Class3.setText("Enter Class Name");
        Class4.setText("Enter Class Name");
        Class5.setText("Enter Class Name");
        Class6.setText("Enter Class Name");
        Class7.setText("Enter Class Name");
  
        Grade1.setText("0.0");
        Grade2.setText("0.0");
        Grade3.setText("0.0");
        Grade4.setText("0.0");
        Grade5.setText("0.0");
        Grade6.setText("0.0");
        Grade7.setText("0.0");
  
        GPALabel.setText("0.00000000000000");
  
      }
  
    }
  
  }
  </code></pre>