Need to make an update to a text file?
If so, I'll show you how to do it right here using Java NIO.
Java NIO, by the way, stands for Java New I/O. It's Oracle's latest means of handling input/output operations with the Java programming language.
And it makes things quite a bit easier than Old School I/O handling.
Let's get started.
Let's say you have the following text file:
Now is the time For all good men To come to the aid Of his party
That file is called contents.txt and it sits in the root.
Your requirement is to add a new line to the end of that file.
The new last line will read "but there's more" once you've completed your update.
Now that you know the requirements, let me show you how to make it happen.
Updating the File
You can handle the update with just a few lines of code. Take a look at this:
final String newLine = System.getProperty("line.separator") + "but there's more"; final Path path = Paths.get("./", "contents.txt"); Files.write(path, newLine.getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
Even though there are only a few lines of code in that block, there's quite a bit happening. Let me break it all down.
For starters, the new line begins with a line separator. That's because the text file that you see above doesn't end with a carriage return.
So without that line separator the new line wouldn't become a new line at all. It would just get appended to the existing last line.
That line separator gets concatenated with the new last line: "but there's more."
Next, the code finds the Path where the file is located. I mentioned above that it's called contents.txt and it's in the root. That fact is reflected in the two parameters in the get() static method of the Paths class.
All the good stuff happens in the third line with Files.write().
That's another static method that lives up to its name. It handles writing to a flat file. In this case, it's writing to a text file.
The first parameter of the write() method specifies the path to the file. That's context.txt in the root.
The second parameter specifies the byte array to write to the file. Yes, even when you're writing to a text file you need to convert it to a byte array.
Even if you choose to write to the file using CharSequence (and you can do that) , the characters will still get encoded into bytes using the UTF-8 charset.
That write() method also lets you specify as many StandardOpenOption choices as you want. In the code above, it's using two options: StandardOpenOption.APPEND and StandardOpenOption.CREATE.
If you're unfamiliar with StandardOpenOption, it's an enum that lets you specify how you open a file.
The APPEND option is fairly self-explanatory. You'll use that option when you want to open an existing file and append to it.
But what about CREATE? What's that doing in there?
It's a failsafe. It's telling the JVM to create the file if it doesn't already exist.
On the other hand, if it does exist, then it will simply append text to the existing file.
In other words, if there's any kind of conflict, APPEND wins.
And that's pretty much it.
Now if you run that code and take a look at the updated contents.txt file, you'll see this:
Now is the time For all good men To come to the aid Of his party but there's more
And that's exactly what you want.
Just keep in mind that if you're using an IDE like Eclipse, you'll probably have to refresh the project to view the updated file contents.
And one last neat observation: note how you don't have to manually close anything after Files.write(). That's because the method itself closes the file once it's completed writing all bytes.
Yeah. Stick with Java NIO.
Wrapping It Up
Now you know how to open a text file and append to it using Java NIO.
It's up to you to make your life easier by using Java NIO instead of the old-fashioned way whenver possible.
Photo by from