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.
- 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
This is a beginner-level course and can be taken by anyone with an interest in learning about Java.
In the previous lecture, you worked on a project involving reading lengths and widths from file and creating rectangle objects from that data. To reinforce your abilities, this lecture uses similar techniques with another geometric shape, the circle. You will use the Circle class that we worked on in the section on classes and OOP. But this time, you'll read the radii of the circles from file and create circles in memory just like you did for the rectangles in the previous project. The file you will read from contains doubles representing the radius of an individual circle on each line and will be named circle_data.txt. This file along with the circle class will be provided with the resources for convenience.
However, now you should print the areas and circumferences of the circle to the console as well as to another file for output called circles_output.txt. You will also need to create a new Java source file named Proj8_2_CircleFile. This is where you'll put your code to do this project. You should try breaking up the program into manageable pieces like we did with the previous project. Make sure you print out the radii and corresponding areas and circumferences to the console and also to the circle_output.txt file. All right. Let's give this a run here, 'Proj8_2_CircleFile'.
I'm going to run this. Right click 'Run' and we get this output here to the console and you should also have created a text file here using your program that gets the output as well, same output. All right. Hope that helps. So, pause the video and come back when you're done or if you need some help. How'd that go for you? Were you able to complete this challenge? Let's work on it together. So, first you'll note that I've already imported the Circle here by copying and pasting the Circle class. So, the same one we had before. And then also I paste it into the top level here, the circle_data.txt file right here. You could of course create your own if you wanted to.
Now, we do have to create the Proj8_2_CircleFiles. So, 'Proj8_2_CircleFile', that creates this class that we'll use to solve our problem. All right. I know that I'm going to be using File, FileNotFoundException. And also since I'm writing this time as well, I'm going to use PrintWriter. Still going to use the ArrayList and still going to use the Scanner for a reading. All right. Similar to before, very similar to before, we have the circleList and then, we could do that on one line of course. And then I will have fillArrayList and printCircles this time. So, very, very similar to the last project. Let me get some extra space here. All right. I guess it doesn't matter if it's private or public. But private_static void fillArrayList.
And then of course, private_static void printCircles that takes an ArrayList of circles again. And of course, from main, since we went to actually test these out, I'm going to do fillArrayList on the circleList that will populate it and then printCircles on the circleList that will print all the elements. So, fillArrayList. I will create a Scanner again in a try catch. end try-catch. Put a little error accessing file. And then in the trisection, infile = new Scanner based on a new File("circle_data.txt"). And then of course we want to close it at the end, Circle temp, double radius, while(infile.hasNext()). I'll grab a radius from each line. So, nextDouble(), just like we did before the length and width with the rectangles.
And then I'll create a temporary new Circle based on that radius. So, no big deal there. Pretty easy. And again to persist the data, we need to add it to the ArrayList. Pretty cool. That's all this method needs. Now down here, with the printCircles, I'm going to create a PrintWriter to have that ready. pw, PrintWriter variable to hold the reference. And then also, I'm going to do a FileNotFoundException. That's what we're going to catch. System.out.println("Couldn't access file or error accessing file") whatever you want to do. And here, remember that we want to loop through the ArrayList. So, I'm going to do for Circle c in the circleAL that was passed in as an argument.
So, end for. And we want to print to the console as well as to our file. Now, before the for loop, I have to create the File. It dawned on me. Notice that red down there. So, I have to create the PrintWriter("circles_output.txt"). There we go. Now the red goes away there on the FileNotFoundException. All righty. So, inside of the for loop, we're going to do a System.out.println ("Radius" is c.getRadius)) println. We're going to do circumference. So, you don't get circumference rather println and then Area, c.area(). And then of course, we'll do this exact same thing. In fact, if you really want to be a little bit sneaky about it,
I'm going to do this actually println() extra here. And I'm going to copy all these just to save some typing and I'm going to change each of these Systems.outs and this lower one to pw. Because the println works essentially the same way as it does for the System.out object as it does for the PrintWriter. And that's exactly what I want. And of course, we're going to run it. So, right click and then run and we do get it to the console of course. Now, what about the file? So, circles_output doesn't look like it printed anything. So, that's not cool. Oh, we did not close the PrintWriter. So, that goes to show you that there was a little mistake and what happens if we don't close it.
So, let's run it again. There you go, same thing to there and then I'm going to open up the circles_output. There we go. That's what we expected right there. Awesome. So, the console output looks pretty good and also the file looks good. Amazing work, everyone. We've learned some pretty powerful techniques in this section and we had a great opportunity to flex our programming and problem-solving muscles with this project. If you'd like a little side challenge, I encourage you to try playing around with the string format method or the printf method of System.out or the PrintWriter to see how to reduce the number of decimal places in the circumference and the area.
So, obviously you've got this right here. That's quite a few decimal places. So, there is a way to clean that up using string format or printf and some other ways too. But in the next lecture, we're going to have our section wrap up. I'll see you there.