Java制作一个txt文件

时间:2011-04-27 03:43:03

标签: java

  

可能重复:
  How to write content on a text file using java?

是否可以使用java将文本字段中的文本保存为纯文本,如果是这样的话?

3 个答案:

答案 0 :(得分: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语句。但如果close()不是finally,您应该writer加入null

答案 1 :(得分:1)

有可能。看看Basic I/O tutorial。您将需要FileWriter

答案 2 :(得分:0)

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

P.S。在使用后务必记得.close()FileWriter。 如果忘记关闭文件编写器,输出可能不会显示。

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