繁体   English   中英

不从文本输入中写入文本文件

[英]Does not write text file from text input

我已经编写了“将文本字段的内容写入文本文件”的代码。 它没有错误,但是没有在文本文件中写入任何内容! 有人可以帮我谈一下我的错误吗? 或编辑代码,然后重新输入整个代码。

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();
   } 
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM