0

您好,我已经编写了一个应用程序来将 TextArea 中的内容写入文本文档,但问题是,他确实写了它,但它没有存储它,当我再次打开它时,在同一个文本文档中写更多内容(或创建一个新文档)。我怎么能那样做?我需要的一个附加信息是如何在我在文本文档中输入的文本旁边添加日期戳?这是我的代码:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;


public class Write extends JFrame {
    JTextArea text;

    public Write() {
        this.setTitle("Writerz!");
        setSize(400, 200);
        setResizable(false);
        setLocation(370, 150);
        setLayout(null);

        JLabel lbltitlu = new JLabel("Insert your text here");
        lbltitlu.setBounds(85, 5, 120, 25);
        this.add(lbltitlu);

        final JTextArea text = new JTextArea();
        text.setSize(199, 199);
        text.setBounds(85, 65, 120, 25);
        add(text);

        JButton btn = new JButton("Add text");
        btn.setSize(99, 99);
        btn.setBounds(125, 125, 120, 25);
        add(btn);

        ActionListener listenerbtn = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO auto- generated method
                String actionbtn = arg0.getActionCommand();

                if (actionbtn.equals("Add text")) {
                    Adauga(text.getText());


                }
            }
        };
        btn.addActionListener(listenerbtn);
    }

    public void Adauga(String text) {
        String filename = "test.txt";

        FileWriter writer = null;
        try {
            writer = new FileWriter(filename);
            writer.write(text);
        } catch (IOException exception) {
            System.err.println("Save oops");
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException exception) {
                    System.err.println("Error closing writer");
                    exception.printStackTrace();
                }
            }
        }
    }


   }

谢谢

4

3 回答 3

0

试试这个

BufferedWriter out = new BufferedWriter(new FileWriter(path));
out.write(text.getText());
out.close();

而不是使用FileWriter. 我用java做了一个记事本,这就是我用的。3行代码,完美运行。对于日期,您可以简单地使用 Date(),导入java.util.Datejava.sql.Timestamp.

Date date= new Date();
Timestamp ts = new Timestamp(date.getTime()));
于 2013-06-19T07:54:57.863 回答
0

你可以试试:

File file = new File("test.txt");

// Create the file if it doesn't exist
if(!file.exists()){
    file.createNewFile();
}

// With true you append
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
于 2013-06-19T07:58:47.517 回答
0

您可以使用以下代码替换您的 Adauga 方法:

public void Adauga(String text) {
    java.util.Date date = new java.util.Date();
    Timestamp ts = new Timestamp(date.getTime());
    BufferedWriter bufferWritter = null;
    try {
        File file = new File("test.txt");
        FileWriter fileWritter = new FileWriter(file, true);
        bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write("Hello: " + " --- " + ts + "\r\n");
        bufferWritter.close();
    } catch (IOException exception) {
        System.err.println("Save oops");
    } finally {
        if (bufferWritter != null) {
            try {
                bufferWritter.close();
            } catch (IOException exception) {
                System.err.println("Error closing writer");
                exception.printStackTrace();
            }
        }
    }
}

注意,您需要导入包 java.sql.Timestamp 和 java.util.Date

于 2013-06-19T08:20:23.873 回答