java里如何点击button后将textfield的输入内容存入创建的txt文件里?

java新手小白,这是我的GUI界面,现在想把textfield里输入的数据在点击save的button的时候可存入创建的txt文件里,应该怎么写呢?求大神,最好讲的详细一些

首先对save按钮增加一个监听事件,然后在监听事件里面进行textfield取值,代码如下:

JButton savebtn = new JButton("save");
savebtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String val= textfield.getText();
        ....增加其他的文本框取值
    }
});

追问

你好,非常感谢你的回答,如果我现在有一个database.txt文件,我想要做的是在点击save按键的时候把所有输入的数据存入这个txt文件中间用空格隔开,需要怎么写程序呢,非常感谢!!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-12-10
直接用getText()获取输入内容,然后用I/O流的形式保存到txt中呀。感觉你的界面好高端的样子,用哪个软件实现的呢
第2个回答  推荐于2016-04-15
将下面的代码放在main函数里面执行一下,将大写转成小写,前提是这个文件存在D:/lin/testfile.txt
public static void ToLowerCase() throws IOException{
File file = new File("D:/lin/testfile.txt");
InputStreamReader in = new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader bufferedReader = new BufferedReader(in);
String lineTxt = null;
String test = "";
while((lineTxt = bufferedReader.readLine()) != null){
test = lineTxt.toLowerCase();
System.out.println(test);
}
FileWriter fw = new FileWriter(file);
fw.write("");
fw.close();
in.close();
OutputStream out = new FileOutputStream(file,true);
out.write(test.getBytes("GBK"));
out.close();
}
//将在控制台中输入的字符保存在txt文件中
public static void save() throws IOException{
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
(new File("D:/lin/testfile.txt")).delete();
//在d盘上创建一个名为testfile的文本文件
File f = new File("D:/lin"+File.separator+"testfile.txt");
//用FileOutputSteam包装文件,并设置文件可追加
OutputStream out = new FileOutputStream(f,true);
out.write(input.getBytes("GBK"));
out.close();
}
第3个回答  推荐于2018-03-04
添加JButton的ActionPerfoirm,里面调用JTextFiled.getText(),结果存入字符串,用File类和outputStream类写入文件。本回答被网友采纳