java中如何把窗体内文本框输入的内容保存到文件内?

java中如何把窗体内文本框输入的内容保存到文件内?

1、获取文本框中的文本

2、将内容写入文件内

具体示例如下所示:

public static void main(String [] args){

// getDatesByYearAndMonth(2015,2);

final JFrame frame = new JFrame("保存文本示例");

JButton button = new JButton("保存到C:/text.txt");

final JTextField text = new JTextField();

frame.add(text,BorderLayout.CENTER);

frame.add(button,BorderLayout.EAST);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

String s = text.getText();//获取文本框中内容

FileWriter out;

try {

out = new FileWriter("C:/text.txt");

out.write(s);//将文本内容保存到文件中

out.close();

JOptionPane.showMessageDialog(frame, "C:/text.txt保存完毕");

} catch (IOException e1) {

e1.printStackTrace();

}

}});

frame.setSize(400, 62);

frame.setVisible(true);

}