Show list of all file names from a folder. How to get list of all files from a folder? Filter the files by file extensions and show the file names. How to read file content using byte array? How to read file content line by line in java? How to read property file in static context? How to read input from console in java? How to get file list from a folder filtered by extensions? How to get file URI reference? How to store and read objects from a file?
How to create and store property file dynamically? How to store property file as xml file? How to get file last modified time? How to convert byte array to inputstream? How to convert inputstream to reader or BufferedReader? How to convert byte array to reader or BufferedReader? How to set file permissions in java? How to read a file using BufferedInputStream? How to create temporary file in java? How to write or store data into temporary file in java?
Both methods have an optional Varargs parameter for setting file handling options see JavaDoc here. The following example would create a non-existing file or append the string to an existing one:. You can use the modify the code below to write your file from whatever class or function is handling the text. One wonders though why the world needs a new text editor 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. 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.
Watch the Windows Notepad, it does not always jump lines, and shows all in one line, use Wordpad ok. 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 ago.
Active 2 months 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 10 more comments. Apache Commons IO contains some great methods for doing this, in particular FileUtils contains the following method: static void writeStringToFile File file, String data which allows you to write text to a file in one method call: FileUtils.
Luis 2, 1 1 gold badge 23 23 silver badges 39 39 bronze badges. Jon Jon Just a minor correction, the second fragment should read: FileUtils. For those of us who prefer Guava, it can do this too. Daniil Shevelev Daniil Shevelev In case someone later wondered, the encoding would be the platform standard.
Note that StandardOpenOption. To use the default just remove the third parameter. Please see Tinus Tate's comment! What is the process to edit this example? I wonder how many thousands of people have taken this example as-is only to find that they have unexpected results when they overwrite a file with a shorter string. In java 11 you can simply put a String as a second parameter! Jorn Jorn XP1 I know, that's a great improvement. Don't forget to call out.
AlexByrth why should he? Large files are recorded in the background another thread and take time to record. Calling flush ensures that everything has been written on the next line, synchronizing the operation. But this is optional , but good practice if you handle large files, as logs. Note that out. Artem Barger Artem Barger So, show your simple and nice solution.
I would be glad to learn how to do it in better way. Ignore the trolls It seems that. Swallowing exceptions like that is going to make life hard for you when exceptions really do occur. At the very least you should rethrow them: throw new RuntimeException e ; — Roger Keays Feb 11 '13 at Show 1 more comment.
Ankur I couldn't disagree more. These libraries are there so we don't introduce subtle bugs in such a simple solution. No, obviously not. I'm only disagreeing that your solution might not be the first thing I'd throw at someone who's a beginner Java programmer. You aren't suggesting that you've never written such a thing, are you?
I have, yes, but that's before I found commons-io. Since finding that, I've never written that sort of thing by hand, even in a one-class project. If I'd known about it from day one, I'd have used it from day one. Exactly, but you're an experienced developer. I'm not disagreeing with the proper use of libraries.
I'm saying that people attempting a language for the first time should try to know it at its bottom, even if that means doing things that they'll discard later on when they're experienced and know better. I implemented this without commons and got a less than obvious exception thrown.
I then implemented this using commons and it told me exactly what was wrong. Moral of the story: why live in the dark ages if you don't have to? Show 3 more comments. The following example would create a non-existing file or append the string to an existing one: Files.