In Java, I have text from a text field in a String variable called "text".
How can I save the contents of the "text" variable to a file?
In Java, I have text from a text field in a String variable called "text".
How can I save the contents of the "text" variable to a file?
In Java 7 you can do this:
There is more info here: http://www.drdobbs.com/jvm/java-se-7-new-file-io/231600403
You can use the ArrayList to put all the contents of the TextArea for exemple, and send as parameter by calling the save, as the writer just wrote string lines, then we use the "for" line by line to write our ArrayList in the end we will be content TextArea in txt file. if something does not make sense, I'm sorry is google translator and I who do not speak English.
Watch the Windows Notepad, it does not always jump lines, and shows all in one line, use Wordpad ok.
private void SaveActionPerformed(java.awt.event.ActionEvent evt) {
}
public void SaveFile(String name, ArrayList< String> message) {
}
I prefer to rely on libraries whenever possible for this sort of operation. This makes me less likely to accidentally omit an important step (like mistake wolfsnipes made above). Some libraries are suggested above, but my favorite for this kind of thing is Google Guava. Guava has a class called Files which works nicely for this task:
In case if you need create text file based on one single string:
Using org.apache.commons.io.FileUtils:
My way is based on stream due to running on all Android versions and needs of fecthing resources such as URL/URI, any suggestion is welcome.
As far as concerned, streams (InputStream and OutputStream) transfer binary data, when developer goes to write a string to a stream, must first convert it to bytes, or in other words encode it.