Java making a txt file [duplicate]
Possible Duplicate:开发者_开发技巧
How to write content on a text file using java?
Is it possible to make use java to save text in a textfield as plain text, if so how?
Yes, get the text from the text field and save it in a file.
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)
I am missing the try/catch/finally
and the import
statements here for brevity. But you should stick the close()
in finally
if writer
is not null
.
It is possible. Take a look at the Basic I/O tutorial. You are going to need a FileWriter
.
using the FileWriter or FileOutputStream(for binary output)
P.S. Always remember to .close() the FileWriter after usage. if you forget to close the filewriter the output might not show up.
By the way, using the BufferedWriter requires .flush() every "output".
精彩评论