2011-04-27 70 views

回答

2

是的,从文本字段中获取文本并将其保存在文件中。

String text = textField.getText(); //get the text from the text field 
BufferedWriter writer = new BufferedWriter(new FileWriter("your_absolute_file_path")); 
writer.write(text); //write it in the file 
writer.flush(); //flush the write-buffer 
writer.close(); //close the writer (and the file) 

我在这里失踪的try/catch/finallyimport语句简洁。但是,如果writer不是null,则应该将close()放在finally中。

+0

我该如何做到这一点,我对Java比较陌生,不知道如何做IO事情 – Mehran 2011-04-27 03:45:47

+0

另请参见File API http://download.oracle.com/javase/1.5.0/docs /api/java/io/File.html – 2011-04-27 03:49:18

0

使用FileWriter的或FileOutputStream中(对于二进制输出)

P.S.请记住在使用后关闭.close()FileWriter。 如果您忘记关闭文件撰写器,则输出可能不会显示。

顺便说一句,使用BufferedWriter需要.flush()每个“输出”。