FileIO_Tickets Program
Description
An external data file FileIO_Tickets_Data.txt contains the ticket sales information for a musical event.The top line is not data, just the caption.
The first column is ticket type, the second column is the unit price, and the third column is the tickets sold.
The purpose of this program is to read data items from the data file, and then calculate the total tickets sold and total revenue.
The results are saved to another file FileIO_Tickets_Result.txt.
Associated Concepts:
File Input and Output
File I/O Exception
File I/O Exception
Algorithm:
Any exceptions related to file I/O are checked exceptions.
It is required to handle them.
Make sure to add throws IOException clause in the main method signature
Create a file output object:
Read the first line (caption) and discard it
Use a while loop to check the end of file indicator (as long as the file has data item):
Read data items one at a time
Calculate revenue
Add revenue to total revenue
Add tickets to total tickets
Remove _ from the ticket category
Save the individual data items to output file
Once the loop is ended, save the total revenue and tickets to the output file.
Close both file input and output objects
It is required to handle them.
Make sure to add throws IOException clause in the main method signature
public static void main(String [] args)
throws IOException {
Create a file input object:
Create a File object and associate it with the data file
Create a Scanner object and associate it with the File object
Create a Scanner object and associate it with the File object
Create a file output object:
Create a FileWriter object and associate it with the output file
Create a PrintWriter object and associate it with the FileWriter object
Create a PrintWriter object and associate it with the FileWriter object
Read the first line (caption) and discard it
Use a while loop to check the end of file indicator (as long as the file has data item):
Read data items one at a time
Calculate revenue
Add revenue to total revenue
Add tickets to total tickets
Remove _ from the ticket category
Save the individual data items to output file
Once the loop is ended, save the total revenue and tickets to the output file.
Close both file input and output objects
Source Code
Sample Run
Output File FileIO_Tickets_Result.txt: