write a class called FileParse.java that will go through a file and and essentia
ID: 3808583 • Letter: W
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:
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.
Explanation / Answer
Hi Student,
Below is the program that contains all the required methods mentioned in the question.
import java.io.*;
public class FileParse {
public static int countWords(String fileName, String searchWord) throws Exception {
String line;
int count=0;
// Read the file
BufferedReader file = new BufferedReader(new FileReader(fileName));
while ((line = file.readLine()) != null) { //Go through the file and tally up the number of matches
String[] words = line.replaceAll("\s+", " ").split(" "); // ignore multiple white spaces
for (String s : words) {
if(s.equals(searchWord))
count++;
}
}
System.out.println("Total number of occurances: " + count); //Print the output
return count;
}
public static boolean deleteLine(String fileName, int lineNum) throws Exception {
BufferedReader file = new BufferedReader(new FileReader(fileName)); // Read the file
File originalFile = new File("fileName");
File tempFile = new File("tempFile.txt"); // creates temp file
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); // creates a BufferedWriter object
String currentLine;
int lineNumber=0;
int flag=0;
while((currentLine = file.readLine()) != null) {
++lineNumber;
if (lineNumber==lineNum) {
flag++;
continue; //skip the next code if linenumber is found
}
writer.write(currentLine);
}
writer.close();
tempFile.renameTo(originalFile); //rename the temp file to original filename
if (flag==0){
System.out.println("Line number not found!"); //print output and return false if not found
return false;
}
else{
System.out.println("Line deleted!"); // print putput and return true if found
return true;
}
}
public static int refactor(String fileName, String original, String replace) throws Exception {
String line="";
int count=0;
// Read the file
RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");
long filePointer = randomAccessFile.getFilePointer(); //fetching the current position of pointer
while((line =randomAccessFile.readLine()) != null){
filePointer = randomAccessFile.getFilePointer() - line.length()-2; //pointing to the initial position in a line
if(line.indexOf(original) > 0){
randomAccessFile.seek(filePointer); //seeking the current position of pointer
randomAccessFile.writeBytes(replace); // replacing the string
count++;
}
}
if (count==0)
System.out.println("Number of words replaced: " + count); //Print the output
else
System.out.println("No Words found");
return count; // returning number of words replaced.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.