gpt4 book ai didi

java - 不从文本输入写入文本文件

转载 作者:行者123 更新时间:2023-12-02 00:12:40 25 4
gpt4 key购买 nike

我编写了“将文本字段的内容写入文本文件”的代码。它没有错误,但它没有在文本文件中写入任何内容!有人可以帮助我并告诉我我的错误吗?或者编辑代码并重新放置整个代码。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:
jButton1.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e){
handleActionPerformed(e);
}
});
}

/**
* @param args the command line arguments
*/

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}

//do you really need to pass ActionEvent in this case?
protected void handleActionPerformed(ActionEvent e) {
BufferedWriter writer = null;
try
{
String text = jTextField1.getText(); //get the text from the text field
writer = new BufferedWriter(new FileWriter("D:\\Definition.txt"));
writer.write(text); //write it in the file
writer.flush(); //flush the write-buffer
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
finally { //always close the stream in finally block
try {
if(writer != null)
writer.close();
}
catch(IOException b) {
b.printStackTrace();
}
}
}

最佳答案

handleActionPerformed(...) 不执行任何操作。我相信这应该有效...

jButton1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
handleActionPerformed(e);
}
}

//do you really need to pass ActionEvent in this case?
protected void handleActionPerformed(ActionEvent e) {
BufferedWriter writer = null;
try
{
String text = jTextField1.getText(); //get the text from the text field
writer = new BufferedWriter(new FileWriter("C:\\Definition.txt"));
writer.write(text); //write it in the file
writer.flush(); //flush the write-buffer
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
finally { //always close the stream in finally block
try {
if(writer != null)
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}

关于java - 不从文本输入写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12413561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com