[英]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.