Practical 11: File I/O
A useful application of classes and exception handling in Java is File I/O. In this practical activity, we will utilize classes specifically meant to carry out file I/O with text files.
Tasks
Task 1
Write a program that reads from Exercise1.txt and counts the number of characters, words, and lines in a file. Words are separated by whitespace characters.
Task 2
Suppose that a text file (Exercise2.txt) contains an unspecified number of scores separated by blanks. Write a program that prompts the user to enter the file, reads the scores from the file, and displays their total and average.
Task 3
Write a program that creates a file named Exercise3.txt if it does not already exist. Write 100 randomly generated integers into this file using text I/O. Integers are to be separated by spaces in the file. Read the data back from the file and display the data in increasing order.
Task 4
Write a program that prompts the user to enter a file name and displays the occurrences of each letter in the file. Letters are case-insensitive. Here is a sample run:
Enter file name: Exercise1.txt
The occurrence of A's is 22
The occurrence of B's is 6
The occurrence of C's is 4
The occurrence of D's is 6
The occurrence of E's is 28
The occurrence of F's is 4
The occurrence of G's is 1
The occurrence of H's is 3
The occurrence of I's is 5
The occurrence of J's is 14
The occurrence of K's is 2
The occurrence of L's is 7
The occurrence of M's is 6
The occurrence of N's is 4
The occurrence of O's is 7
The occurrence of P's is 4
The occurrence of Q's is 0
The occurrence of R's is 15
The occurrence of S's is 16
The occurrence of T's is 5
The occurrence of U's is 6
The occurrence of V's is 6
The occurrence of W's is 1
The occurrence of X's is 0
The occurrence of Y's is 7
The occurrence of Z's is 0