从Java应用程序存储并添加新行+日期戳到文档

时间:2013-06-19 07:51:46

标签: java document

你好我已经编写了一个应用程序来编写一个从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();
                }
            }
        }
    }


   }

由于

3 个答案:

答案 0 :(得分:0)

试试这个

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

而不是使用FileWriter。 我在java中创建了一个记事本,这就是我使用的内容。 3行代码,效果很好。 对于日期,您只需使用日期(),导入java.util.Datejava.sql.Timestamp

Date date= new Date();
Timestamp ts = new Timestamp(date.getTime()));

答案 1 :(得分: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();

答案 2 :(得分: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