Join Peggy Fisher for an in-depth discussion in this video Solution: Loading a file, part of Learning Java (2015).
- This latest challenge was designed to give you practice with reading and writing files. Let's take a look at my solution and, as always, remember that your solution might be very different. Also, I do wanna point out if your program seems to work but the output file is empty every time, check to make sure you close the file before exiting your program. Okay, at the top of my program we have our import statements. We import jave.io.* to allow us to read and write from files.
In the main part of the program, the first thing I did was to clear my input file, File menuItems = new File ("menuNoPrices.txt"). I'll show you the file real quickly. I'll go to Windows Explorer. I'm already in my FileChallenge, and here is my menuNoPrices. If I open it up, you can see I have eight different menu items. Okay, let's go back to the program. I'm also creating my file for the output. This one's going to be called menuWithPrices.txt.
That file doesn't exist yet, but because it's an output file, the program will go ahead and create it. I'm gonna scroll down a little bit. I have a String variable to hold my menuItem and a double variable to hold the price. Here is my try and catch. I wanna make sure I handle any exceptions. The first thing I did was create a Scanner object called input, which will represent the menu items in the input file. Right below that I have a PrintWriter statement that's creating a variable output that represents my output file menuPrices.
I still need to have Scanner in = new Scanner (System.in) because I wanna ask the user to enter in the prices. I'm using the while loop, which will read from the input file until there is no more menu items. I'm reading the first line in the file into the variable called menuItem, then I'm asking the user, what is the price of this item? I read the price in. Because the price is a number, it won't read in the end line symbol, so I have to get rid of that using in.nextLine.
Then I'm printing the menuItem out to my output file. I use the escape sequence \t, and I'm also appending the price to the same line. I'm gonna scroll down a little bit. This line right here, line 36, as I stated, is really important. I have to close my file, otherwise all the information will be left out in the buffer. And, of course, I'm catching my Exception error and printing it out if there is any problems. Let's try and run this program. You can see it read Onion Rings from the input file, so I'll put a price for Onion Rings, $2.5.
I'll put a price for each item. Soda was the last item in the list, so I put in the price, $1.75, and my program ended. Now, let's open up our output file and see what it looks like. I'm already in the right folder, I'm in the FileChallenge folder, and there it is, menuWithPrices. Let's open it up. As you can see, it appended the price of each item into my file. It looks like it worked. Okay, hopefully you had fun with this activity and learned more about reading and writing to files.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs