How to Write Data to a File Using Java

Java


In this article, we show how to write data to a file using Java.

With Java, you can write data to many different types of file formats such as text formats.

In Java, we can write to a file by creating an object of the PrintWriter function and then using out.print or out.println statements to write contents to the file.

The code to write data to a file using Java is shown below.



So in the file above we create a class called Writefile.

We then create our main method(), which is the point where execution of our program begins.

In the main method, we create a file object, f, and pass the parameter named "filetowrite.txt" into the this File object.

So at this point in the code, the File object f has the value of filetowrite.txt.

We then use a try function to try a portion of code. If it doesn't work, exception handling is done.

To write a file, you have to create a PrintWriter object. We create a PrintWriter object named out.

We then output the line, "Java is a great programming language."

This is written (output) to the file, filetowrite.txt, by the out.println() function.

Being that we set out to be an object of the PrintWriter function, when we use the statement, out.println(), we are referencing the PrintWriter object(which is set to filewrite.txt). So whatever we pass into the argument of the out.println() function gets written to the file that we specified.

If you use the term, System.out.println, this will not write the contents to the file. Instead, it will write the contents to the console. So you do not want to make the mistake of using System.out.print(ln) statements to write data to a file. Instead, out.println statements write contents to a file.

When you execute the above file, you won't see any output in the console. All the data passed into the out.println() function is seen in the file you are writing to.

When you are writing to a file, you must close the file object. This is done through the statement, out.close(). If you do not have this statment, the program will not work.

When dealing with the PrintWriter objects in Java and writing data to a file, you need to perform exception handling. This is because there are many reasons why errors may occur. For example, you may specify a pathway that doesn't exist or a file type that Java can't write to, etc. For this reason, Java looks for exception handling when dealing with PrintWriter objects. We provide this through try and catch functions.

Since we didn't specify the path of the file, the Java program creates the new file in the current directory, the one where this Java file saves to.

If you want to specify a directory other than the current directory, then you will have to specify the complete path to the file.

Since this differs according to what operating system you are using, this is the only part of Java that becomes platform dependent.

In a Windows computer, you normally specify the drive you are using with a letter such as the C drive and then specify the path from there to the file that you want to write to.

So if you have a file you want to create in the C:\Users\ directory, then you would write this into the string as "C:\\Users\\"

This is because "\" is an escape character; it's used for escaping strings. To show "\" in a string, you would have to put double forward slashes "\\".

So the above code would be modified to the following.



Appending Data to a File

The code above writes data to a file and wipes out all existing data that may be in a file.

In other words, it completely writes over everything that may be in a file and replaces it with the data you pass into it.

This is fine, if you're starting off a fresh blank document.

However, if you have something already in a file and you want to append additional things to it, the above code is horrible.

To append data to a file that has already has content requires a slight modification to the above code.

We have to change the PrintWriter out object.

This is shown below.

This is the only line that you have to change to the above code. When you pass ture into the FileWriter object, this means that you want to append the data you are passing into the file, not write over the data in the file.

So if true is either left out or if the parameter is false, the program will completely erase all data in the file and write it over with whatever you pass in. If the parameter is true, then the program will append the data you pass in to the already existing contents in the file.

And this is all that is required to write data to a file using Java.


Related Resources

How to Check if a File Exists Using Java

How to Check if an Object is a File Using Java

How to Check if a File is a Directory Using Java

How to Create a New File Using Java

How to Rename a File Using Java

How to Delete a File Using Java

How to Create a New Directory Using Java

How to List All Files in a Directory Using Java

How to Read a File Using Java

How to Get the Size of a File Using Java




HTML Comment Box is loading comments...