Question
write a class called FileParse.java that will go through a file and and essentially parse that file. The FileParse class should contain the following methods:
write a class called FileParse.java that will go through a file and and essentially parse that file. The FileParse class should contain the following methods:
public static int countWords(String fileName, String word)
This method goes through a file and finds occurrences of the given word and keeps track of the number of times the word appears in the file.
Then the method will print out a message displaying the result.
Total number of occurrences: 15
Finally the method returns the number of occurrences as an int variable.
For the purposes of this homework you are safe to assume there is only one occurrence of the word per line.
public static boolean deleteLine(String fileName, int lineNum)
A method that goes through a file and deletes a particular line on the file where the line number is given .
Once deletion is completed, the following message is displayed on the console:
Line deleted!
If the line number does not exist, you will print
Line not found
Return true if the deletion was a success, false otherwise.
public static int refactor(String fileName, String original, String replace)
A method that goes through the file and replaces a word with a different word that was given to you.
Once the replacement is done, a message in following format is displayed on the console showing how many words were replaced:
Number of words replaced: 15
If no words were found, print
No words found
Return the number of words replaced as an int variable.
import java.io.*; public class FileParse { public static int countWords(String fileName, String searchWord) throws Exception { //TODO: Open file with the path fileName //TODO: Read the file //TODO: Go through the file and tally up the number of matches //TODO: Print output } public static boolean deleteLine(String fileName, int lineNum) throws Exception { //Create an array to save the lines //TODO: Iterate through the file line by line until you find the 'lineNum' line //If line is found, skip saving it, otherwise, save it in the array //Write the whole array to the file //TODO: Print Line deleted! if success //TODO: Print Line not found... otherwise } public static int refactor(String fileName, String original, String replace) throws Exception { //TODO: Iterate through the file line by line and check if the line has the original string //Replace the original with replace //HINTS: Use RandomAccessFile //TODO: Print output } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started