可能重复:
How to write content on a text file using java?的Java做一个txt文件
是否有可能利用Java来保存在一个文本框为纯文本的文本,如果是的话怎么样?
可能重复:
How to write content on a text file using java?的Java做一个txt文件
是否有可能利用Java来保存在一个文本框为纯文本的文本,如果是的话怎么样?
是的,从文本字段中获取文本并将其保存在文件中。
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/finally
和import
语句简洁。但是,如果writer
不是null
,则应该将close()
放在finally
中。
我该如何做到这一点,我对Java比较陌生,不知道如何做IO事情 – Mehran 2011-04-27 03:45:47
另请参见File API http://download.oracle.com/javase/1.5.0/docs /api/java/io/File.html – 2011-04-27 03:49:18
这是可能的。看看Basic I/O tutorial。你将需要一个FileWriter
。
使用FileWriter的或FileOutputStream中(对于二进制输出)
P.S.请记住在使用后关闭.close()FileWriter。 如果您忘记关闭文件撰写器,则输出可能不会显示。
顺便说一句,使用BufferedWriter需要.flush()每个“输出”。
你是说在文本字段或文本文件中保存文本? – CoolBeans 2011-04-27 03:47:25
您可能希望在提问前搜索该网站,这可能已经回答了100次。 – JohnFx 2011-04-27 03:49:25