1

试图写入文件。我做了研究,确保冲洗并关闭。我以为它可能正在读取空白消息框并且什么也没写,但是做了一个 System.out.println 以确保它正确读取,并且确实如此。所以现在不确定。

原始代码:

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

public class Form extends Applet implements ActionListener{
private TextField first = new TextField("First Name",12);
private TextField middle = new TextField("I",1);
private TextField last = new TextField("Last Name",15);
private TextField address = new TextField("Address",25);
private TextField city = new TextField("City",20);
private TextField state = new TextField("State",2);
private TextField zip = new TextField("Postal Code",10);
private Button submit =new Button("Submit");
private Button reset = new Button("Reset");
private Button save = new Button("Save");
private Button enlarge = new Button("Enlarge");
private TextArea message = new TextArea();


public void init()
{
    GridBagLayout gbl = new GridBagLayout();
    setLayout(gbl);
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.weightx=1.0;
    c.weighty=1.0;
    c.fill=GridBagConstraints.HORIZONTAL;
    c.insets=new Insets(5,5,5,5);
    gbl.setConstraints(first,c); add(first);
    c.fill=GridBagConstraints.NONE;
    gbl.setConstraints(middle,c); add(middle);
    c.fill=GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(last,c); add(last);
    c.gridy=1;
    gbl.setConstraints(address,c); add(address);
    c.gridy=2;
    gbl.setConstraints(city,c); add(city);
    c.fill=GridBagConstraints.NONE;
    gbl.setConstraints(state,c); add(state);
    c.gridy=3;
    gbl.setConstraints(zip,c); add(zip);
    c.gridy=4;
    c.anchor = GridBagConstraints.CENTER;
    gbl.setConstraints(submit,c);add(submit);
    gbl.setConstraints(reset,c); add(reset);
    c.gridy=5;
    c.anchor = GridBagConstraints.CENTER;
    gbl.setConstraints(save, c); add(save);
    gbl.setConstraints(enlarge, c); add(enlarge);
    c.gridy=6;
    c.gridwidth=3;
    c.fill=GridBagConstraints.BOTH;
    gbl.setConstraints(message,c);add(message);
    submit.addActionListener(this);
    reset.addActionListener(this);
    enlarge.addActionListener(this);
    save.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{

    String initial = middle.getText();
    String a=message.getText();
    if (initial != null)
        initial +=". ";
    Object source = event.getSource();
    if (source == submit)
    {

        message.setText(first.getText()+ ' ' +initial+last.getText());
        message.append('\n'+address.getText());
        message.append('\n'+city.getText()+", " +state.getText());
        message.append(' ' +zip.getText());
        message.append("\n\nYour message:\n"+a);
    }
    else if (source == reset)
    {
        message.setText("");
        first.setText("");
        middle.setText("");
        last.setText("");
        address.setText("");
        city.setText("");
        state.setText("");
        zip.setText("");

    }
    else if (source == enlarge)

    {
        message.setFont(new Font("Serif", Font.PLAIN, 14));
        message.setForeground(Color.BLUE);
        message.setText(a);
    }
    else if (source == save)
    {
        try{


        File file=new File("savedMessage.txt");
        PrintWriter pw = new PrintWriter(file);
                pw.println(message.getText());
                //message.setText("Print was successful")
                pw.flush();
                pw.close();
                System.out.println(message.getText());
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());

        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());
        }

    }

}

}

这是用于解决问题的代码:

    else if (source == save)
    {
        try{
            f = new File("savedMessage.txt");
            bw = new BufferedWriter(new FileWriter(f,true));
            bw.write(message.getText());
            if(f.canWrite()){
            System.out.println("Success");
            }
            }

        catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());

        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());
        }
    finally{

            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
4

2 回答 2

3

PrintWriter吞下异常。请参阅 Javadoc。你需要打电话checkError()。更好的是,使用BufferedWriter它不会吞下它们。除非您的小程序已签名,否则您可能会遇到苹果沙盒权限问题。

于 2012-12-10T00:27:45.497 回答
-1

第一个提示:关闭文件!在 catch ioexception 下添加一个 finally 块

    File file = null;
    try {
       file = new File("filename.txt");
    } catch (IOException ex) {

    } finally {
       try {
            file.close();
        } catch (IoException ex2) { }

    }

进一步检查文件是否可写

if (!file.canWrite()) {
// forget it, cannit write
 }
于 2012-12-10T00:25:03.570 回答