开发者

Java making a txt file [duplicate]

开发者 https://www.devze.com 2023-03-01 02:47 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate:开发者_开发技巧 How to write content on a text file using java?
This question already has answers here: Closed 11 years ago.

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消