This course covers how to persist data as well as how to write data from programs to file to be used later, which is called file output. We'll also learn about how to load the data from files and populate our variables with that data, which is called file input.
Intended Audience
- Beginner coders or anyone new to Java
- Experienced Java programmers who want to maintain their Java knowledge
- Developers looking to upskill for a project or career change
- College students and anyone else studying Java
Prerequisites
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous lectures, you've learned about how to interact with files, including using them for input as well as output. In this lecture, you'll dive into your first project of the section. This time, you will use the same Rectangle class that we originally developed in the section on classes and OOP. The class file along with a text file rectangle_data.txt will be provided as a resource with this lecture. You'll copy in the Rectangle.java file containing our Rectangle class from earlier, into the Section8-Projects IntelliJ project under the src directory. Additionally, you will copy in the input text file named rectangle_data.txt into the project. Then, once you've done this, create a source file named Proj8_1_RectangleFile.
The file contains data about different rectangles, with each line representing a single rectangle object. The format of each line in the input file is length, followed by a space, followed by the width, with both pieces of data consisting of whole numbers and possibly having decimal parts. The data will be read in from file into memory and you will create Rectangle objects from the data. Each time you create a Rectangle object, add it's reference to an ArrayList. When the file is done being read, don't forget to close it. Then using the ArrayList, loop through and print out the length, width, area, and perimeter of each Rectangle object referenced in the ArrayList to the console.
You may want to write some additional methods to make the code cleaner and more manageable into practice writing methods. All right, let's run this file so we get an idea of what it should look like, the output. So, I right click it and run it and pretty straightforward you just need the length, width, area, and perimeter and then I added some extra space down here for each of the rectangles that were read from the file. So, pretty cool. Hope that helps. So, pause the video and give this project your best shot. Come back when you're done or if you need some help. How did that go for you? Were you able to complete this challenge? Let's work on it together.
So, you noticed I have already copied in the rectangle_data.txt file, which I just did a Control+C or copy, Command+C on Mac and I pasted it into this project right here. I did the same thing to rectangle but I pasted it into the src subfolder right here. This is the same rectangle file that we used before when we developed our class Rectangle here, so you might review it and look over it and we can see that it has length and width and all the other stuff that we expect the rectangle to have as we developed it. And of course, the rectangle_data.txt has length space width, length space width, etc. And there's 10 elements in this, but you shouldn't code to just 10 values. It should be able to handle 5, 10, 100 doesn't really matter.
So, once we have those two, you then need to make sure that you create a source file. Right click, 'New', 'Java Class', Proj8_1_RectangleFile. There we go. And of course, our little skeleton program, public static void main. Here we go. Since we're dealing with files, I know that we're going to need java.io.File, java.io.FileNotFoundException, and we're going to need the ArrayList. So, java.util.ArrayList and java.util.Scanner since we're reading in from the file. So, first of all, I'm going to create an ArrayList of Rectangles right here that I'll call rectangleList and I'll create actually instantiate the rectangleList right here.
And I'm going to create two little helper methods that I'm going to call in just a moment. But first, let's do this. So, public static this is outside of main clearly. fillArrayList and it takes an ArrayList of rectangles, rectangleAL and I'll create another one, getting that one ready. Static void printRectangles, this one is just going to print them; rectangleAL there we go. Then back up in main just so I don't forget it, I'm going to call these two methods. So, we'll do fillArrayList and we'll pass at the rectangleList here. And we'll also call printArrayList or printRectangles rather, and I will pass it rectangleList as well. So, these currently aren't doing anything. You can run the file and test it out.
It's not doing anything. We have to tell it what to do. So, inside of our fillArrayList I know that I'm going to need a scanner which I'll call infile and I'm going to set up a try catch down here because I know that the scanner has a checked exception when we try to read from file. So, here's where I'm going to create it, infile equals new Scanner based on the new file. And I'm just going to look for rectangle_data.txt. And then of course before you do anything else, make sure you close it. That's very important. Remember to do that. And I'm going to do this, I'm going to create Rectangle temp. So, a reference to a Rectangle object, double length and double width. And of course let me actually push this up a little bit.
So, I have some extra space to work with, we have double length and double width and while infile.hasNext(). So, as long as we're reading the file, I'm going to grab the length infile.nextDouble() and the width infile.nextDouble(). So, since each line has double value space double value, this will grab the data in an entire line. Another technique you may have chosen to use would be to just create a string, grab the entire line and then split it and then parse the two values into a double. But this is pretty straightforward and easy. So, I'm doing it this way. Now, once you have the length and width you don't have a rectangle yet. So I'm going to create using this temp reference, I'm going to say new Rectangle(length, width).
There we go. I've got my rectangle in memory but the problem is if I keep looping, this is actually going to keep reading new values and keep creating new rectangles. But temp is going to be assigned new value. The old object's reference will be lost and then that old object will die, it will get garbage collected. So, how do we persist this data during the execution of our program? And the answer is we're going to take the ArrayList that was passed in called rectangleAL, and add the reference. So, even though temp keeps getting written over rectangleAL will add a new component to it's list and then it will be able to keep track of it so that we can print them out when we need them later. Pretty cool. So, of course in the catch I'm going to put something like "Error accessing file!". There we go and then print rectangles. Little more straightforward. Little easy.
So, this one probably you're able to do with your eyes closed but maybe not. That's okay too. So, I'm using an enhanced for loop, you could use a regular for loop and then just go to rectangleALs based on rectangleAL size. If you'd like to do that System.out.printIn, I'm going to say length because we want to print out the length, width, area and perimeter. So, I'll say r.getLength(), r.getWidth(), r.area(). I'm gonna fix that there okay. And perimeter, r.perimeter(). There we go, spelling is useful. Now, I'm going to add an extra additional line at the bottom just so it add some more spacing and of course let's run this whole thing. So, I'm going to right click and go to run. There we go.
That's what we expected. So, we've got the length with area and perimeter for each of the rectangles being printed out. Pretty awesome. Although, it might not seem so amazing. This code does something very powerful. We have read rectangle data from file and our program doesn't need to know how many rectangles are represent. It can handle 5, 10, 100, 1000 or more rectangles that is extremely powerful and extremely awesome. As a side challenge, maybe you could try variations on this project for extra practice. Maybe try to print the output for the rectangles to a file in addition to us printing it to the console. In the next lecture, we'll work on another project. The project will be similar to this one, where you'll create circle objects from the data in a file. Let's get going.
John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.