InputStreams和OutputStreams

时间:2012-06-25 19:34:37

标签: java inputstream outputstream

我写了这个简短的程序,它有一个很小的GUI。它应该允许您键入文本,然后您可以将其保存为txt文件或从以前保存的文件中读取或删除现有文件。 它有效,但有一些奇怪的错误。有时在创建文件后,如果我尝试删除它,它将不会删除该文件。

另外,我完全不确定我是否以良好的方式编写代码。 我想知道你是否可以看看我写的东西,发现我应该写不同的弱点或区域。也许你可以看到为什么在创建文件后删除文件有问题。我真的很茫然,我不知道该问谁。 这是我的代码:

public class InputOutout extends JFrame{

    private static final long serialVersionUID = -7073762217756427192L;

    JLabel label;
    JTextField tf;
    JButton buttonAdd;
    JButton buttonDisplay;
    JButton buttonErase;

    public InputOutout() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }

        setLayout(new FlowLayout());

        label = new JLabel("Enter text");
        add(label);

        tf = new JTextField(10);
        add(tf);

        buttonAdd = new JButton("Press to save to file");
        add(buttonAdd);

        buttonDisplay = new JButton("Press to display file content");
        add(buttonDisplay);

        buttonErase = new JButton("Press to erase file");
        add(buttonErase);

        eventAdd add = new eventAdd();
        eventDisplay remove = new eventDisplay();
        eventErase erase = new eventErase();

        buttonAdd.addActionListener(add);
        buttonDisplay.addActionListener(remove);
        buttonErase.addActionListener(erase);       
    }

    private class eventAdd implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                String word = tf.getText()+ " ";
                FileWriter stream = new FileWriter("text.txt",true);
                BufferedWriter out = new BufferedWriter(stream);
                out.append(word);
                out.close();
                tf.setText("");
            } catch (Exception ex){
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }

        }
    }

    private class eventDisplay implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                FileReader stream = new FileReader("text.txt");
                BufferedReader in = new BufferedReader(stream);
                String text = in.readLine();
                JOptionPane.showMessageDialog(null, text);
                stream.close();
            } catch (Exception ex){
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }

        }
    }

    private class eventErase implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                File file = new File("text.txt");
                boolean success = file.delete();

                if (!success) {
                    JOptionPane.showMessageDialog(null, "File was not deleted");
                } else {
                    JOptionPane.showMessageDialog(null, "File was deleted");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null,ex.getMessage());
            }
        }
    }

    public static void main(String[] args) {

        InputOutout gui = new InputOutout();

        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(350,100);
        gui.setTitle("SuperWORD 2013");
        gui.setVisible(true);       
    }
}

1 个答案:

答案 0 :(得分:2)

围绕file.delete()的逻辑不太正确,因为true只有在删除时才会返回。这意味着如果file.delete()不存在,false将返回File file = new File("text.txt"); if (file.exists()) { if (!file.delete()) { JOptionPane.showMessageDialog(null, "File was not deleted"); } else { JOptionPane.showMessageDialog(null, "File was deleted"); } } else { JOptionPane.showMessageDialog(null, "File does not exist"); } 。在尝试删除文件之前检查文件是否存在:

try {} catch() finally {}

建议使用BufferedReader.close()构造来确保调用BufferedWriter.close()try { BufferedWriter out = new BufferedWriter(new FileWriter("text.txt",true)); try { String word = tf.getText()+ " "; out.append(word); tf.setText(""); } finally { try { out.close(); } catch (IOException ex) { // Report close failure. JOptionPane.showMessageDialog(null, ex.getMessage()); } } } catch (Exception ex) { // Report open or write failure. JOptionPane.showMessageDialog(null,ex.getMessage()); }

{{1}}