Wednesday, 31 May 2017

Tutorial 30: Writing Files



Writing files is a lot similar to reading files as you will see shortly. So, when would you want to write files in Java? Well, you probably want to save some information on the computers of the people using your software so that the information can be retrieved later. 

In this tutorial, we are going to use the same syntax we used in the tutorial on reading files. We are going to use the Java 7 try with resources syntax. Why? Because it makes code look neater, we type less code and we don’t have to close the BufferedReader object to prevent resource leaks. The try with resources syntax automatically calls the close() function on the file handle.
We always start by creating a File object, which contains the location that we would like the file to be located.

File file1 = new File (“C:/Users/Mukami/Desktop/Document1.txt”);

Then, we create an object of the type FileWriter and pass on the object of type File to its constructor like this:

FileWriter filewriter1 = new FileWriter(file1);

Then, we create an object of the type BufferedWriter and pass the object of the type FileWriter to it like this:

BufferedWriter bufferedWriter1 = new BufferedWriter(fileWriter);

Now, as we saw in the tutorial on the “try with resources” syntax, we can compress all the above steps into one line like this:

BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(file));

So now, we can enclose the above statement with the try with resources construct like this:

try(BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(file))){

} catch (IOException e) {
            System.out.println(“Could not write the file: ” + file1.toString());
}

Great! Now the remaining step is to write the functionality to actually write the information to the file within the above try block. To write information, we will use the write() method of the BufferedWriter class. To add a newline, we will use the newLine() function of the BufferedWriter class.

try(BufferedWriter bufferedWriter1 = new BufferedWriter(new FileWriter(file))){
            bufferedWriter1.write(“Writing the first line.”);
            buffereWriter1.newLine();
            bufferedWriter1.write(“Writing the second line.”);
            bufferedWriter1.newLine();
} catch (IOException e) {
            System.out.println(“Could not write the file: ” + file1.toString());
}


Figure 1: Results of executing the above program to write to a file.
 

That’s all with regards to writing text files in Java. Note that by using the try with resources syntax, Java will automatically call the close() function. Note that the code above is more compact and neater than what we wrote when we were first learning to read files with pre-Java 7 syntax.

If you have any question regarding this tutorial or any other tutorial in this series, please make sure to drop them in the comments section below and I will address them.

In the next tutorial, we are going to look at the equals method.
Thank you so much and until next time, stay safe. 

No comments:

Post a Comment