Results 1 to 2 of 2
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question Why won't this write to file correctly

    This code works, and writes to a file on my desktop.
    Java Code:
    import java.io.*;
    public class writetofile
    {
    public static void main(String[] args) throws Exception
    {
    File tempfile = new File("C:/users/zlloyd1/desktop/text.doc");
    FileWriter writer = new FileWriter(tempfile, true);
    writer.write("test text");
    writer.flush();
    }
    }
    This is a proven fact, as I have run this several times. It writes to the file text.doc on the desktop, and creates that file if there is not one there.
    I have tried to use the same strategy to get another program I am working on to read from and write to an external text file, but unfortunately, now the compiler is acting like it has never heard of this code before.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    public class newgui extends JFrame{
    private JPanel panel;
    private JTextField txt1;
    File tempfile = new File("C:/users/zlloyd1/desktop/text.doc");
    private JButton b1, b2;
    
    private JTextArea txtarea1;
    public newgui(){
    setLayout(new FlowLayout());
    panel = new JPanel();
    add(panel);
    txt1 = new JTextField(24);
    b1 = new JButton("Exit");
    b2 = new JButton("send");
    panel.add(b1);
    panel.add(b2);
    panel.add(txt1);
    txtarea1 = new JTextArea(8,24);
    //txtarea1.setLocationRelativeTo(null);
    add(txtarea1);
    event e1 = new event();
    b1.addActionListener(e1);
    event e2 = new event();
    b2.addActionListener(e2);
    }
    public class event implements ActionListener {
    public void actionPerformed(ActionEvent e1){
       System.exit(0);
    }
    }
    public class event2 implements ActionListener { // it seems to have a real problem with this code block??
    public void actionPerformed(ActionEvent e2){
    String txtreada = txt1.getText();
    FileWriter writer = new FileWriter(tempfile, true);
    writer.write(txtreada);
    writer.flush();
    }} //no problems after this line....
    public static void main(String[] srgs){
    newgui gui = new newgui();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(480,480);
    gui.setVisible(true);
    gui.setTitle("Title");
    }}
    The errors I am getting say, "unreported exception IOException" on lines 38, 39, and 40, but I am clueless to what it means by that, because as I stated, this code is checked and tested to be correct??
    Am I using it wrong here or what??
    PLEASE HELP!!

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why won't this write to file correctly

    Look at your first example: you told the compiler that the main( ... ) method could throw an Exception (An IOException is an Exception), but you didn't do so in your second example; the compiler, being a nasty bean counter, complains about it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 07-25-2012, 10:00 AM
  2. Replies: 6
    Last Post: 12-01-2010, 05:31 AM
  3. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  4. How correctly open text file?
    By artemff in forum CLDC and MIDP
    Replies: 3
    Last Post: 12-23-2009, 06:39 PM
  5. Replies: 5
    Last Post: 02-05-2009, 10:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •