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:. You can insert this method into your classes.
If you are using this method in a class with a main method, change this class to static by adding the static key word. Either way you will need to import java. I think the best way is using Files.
See javadoc :. Write lines of text to a file. Each line is a char sequence and is written to the file in sequence with each line terminated by the platform's line separator, as defined by the system property line. Characters are encoded into bytes using the specified charset. The options parameter specifies how the the file is created or opened. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0.
Please note. I see people have already answered with Java's built-in Files. String , instead of a byte[] array, thus text. If you wish to keep the carriage return characters from the string into a file here is an code example:. 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.
How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How do I save a String to a text file using Java? Ask Question. Asked 12 years, 6 months ago. Active 16 days ago. Viewed 1. 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? Improve this question. Justin White Justin White 7, 4 4 gold badges 17 17 silver badges 9 9 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Jeremy Smyth Jeremy Smyth Justin, you could also pass an absolute path e.
Btw, this could be simplified using the convenience constructors PrintStream has had since 1. Need to close that file though at some point? Show 9 more comments. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use , cookie and privacy policy. Copyright by Refsnes Data. All Rights Reserved. Using IDEs to spit out verbose code i don't this the above is verbose was called 'code vomit' by a nice scala speaker once ;.
Hi, You have written very nice article. I acquired good knowledge about Java. I will follow up your blog for future post. Only this little change was [ I've download the mocked project and launch the[ I know several people are [ How could something so simple as a bunch of characters repr[ This method comes in two variants. The most basic form requires a Path of the file to write to and the textual contents. The other variant also accepts an optional CharSet :.
There is little room for flexibility here, but it works great if you need to write something down into a file quickly. A String, like other objects, can be converted into a byte[]. The Files. FileWriter is one of the simplest ways to write some textual contents into a file.
We'll create a File instance and pass it into the FileWriter constructor to "bridge" them. After using the writer, it's important to flush and close the resources. Alternatively, you can do this with the try-with-resources syntax:.
0コメント