The following code is supposed to take data entered into a text field, and send it to an external file. Then it is supposed to retrieve that data from the file and read it into a text area on the frame.
It works, mostly.... Unfortunately, for some reason that I have been unable to see, it will only retrieve the last word from the file??
For instance, if I enter xxxx1234 into the text field, and click b3, it will put xxxx1234 in the text area, but if I write xxxx 1234 into the text field and click it, I will only get back 1234??
Can someone tell me why I am only getting the last word, Please??import java.awt.event.*; import java.awt.*; import java.util.*; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.*; public class Interface implements ActionListener{ JFrame frame = new JFrame("Interface program"); JTextField txt1 = new JTextField(16); JTextArea txt2 = new JTextArea(24, 24); JButton b1, b2, b3; JLabel label1, label2; public void creata() { frame.setLayout(new FlowLayout()); b1 = new JButton("b1"); b2 = new JButton("Exit"); b3 = new JButton("b3"); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); frame.add(b1); frame.add(b2); frame.add(b3); frame.add(txt1); txt1.setEditable(true); label1 = new JLabel("enter data"); frame.add(label1); frame.add(txt2); txt2.setEditable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Week 3 Interface"); frame.setSize(600, 600); frame.setVisible(true); } public static void main(String[] args) { Interface intr = new Interface(); intr.creata(); } @Override public void actionPerformed(ActionEvent ae) { //throw new UnsupportedOperationException("Not supported yet."); if(ae.getSource() == b2){ System.exit(0); }else if (ae.getSource()==b1){ FileWriter writer = null; try { String txtread = txt1.getText(); File tempfile = new File("C:/users/zlloyd1/desktop/newtesta.txt"); writer = new FileWriter(tempfile, true); writer.write("" + txtread); } catch (IOException ex) { Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex); } finally { try { writer.close(); } catch (IOException ex) { Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex); } } }else{ try{File file = new File("C:/users/zlloyd1/desktop/newtesta.txt"); Scanner scan = new Scanner(file); while(scan.hasNext()){ txt2.setText(scan.next()); } }catch(Exception ex){ String tempa = ex.getMessage(); } }} }