我寫了一個代碼「將文本內容寫入文本文件」。它沒有錯誤,但它不會在文本文件中寫入任何內容!任何人都可以幫助我並告訴我的錯誤嗎?或編輯代碼並重新輸入整個代碼。不寫文本文件從文本輸入
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'這樣你就可以知道它實際上是獲取調用。 – DanielGibbs