Question:
Consider the following scenario:
You have been given a task to create a Java program that reads a text file, performs some operations, and writes the result to a new file. As part of this task, you need to utilize the file classes provided in Java. Write a program that reads a text file named "input.txt", calculates the frequency of each word present in the file, and writes the result to a new text file named "output.txt".
Write a Java program that accomplishes the above task. Your program should satisfy the following requirements:
calculateWordFrequency(String fileName)
that takes the name of the input file as a parameter and returns a HashMap<String, Integer>
containing the frequency of each word.writeToFile(HashMap<String, Integer> wordFrequency, String fileName)
that takes the word frequency map and the name of the output file as parameters, and writes the word frequency data to the output file.Provide the complete implementation of the Java program.
Answer:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class WordFrequencyCalculator {
public static void main(String[] args) {
String inputFileName = "input.txt";
String outputFileName = "output.txt";
try {
HashMap<String, Integer> wordFrequency = calculateWordFrequency(inputFileName);
writeToFile(wordFrequency, outputFileName);
System.out.println("Word frequency calculation completed and written to " + outputFileName);
} catch (FileNotFoundException e) {
System.out.println("Input file not found: " + inputFileName);
} catch (IOException e) {
System.out.println("An error occurred while writing to the output file: " + outputFileName);
}
}
public static HashMap<String, Integer> calculateWordFrequency(String fileName) throws FileNotFoundException {
HashMap<String, Integer> wordFrequency = new HashMap<>();
File inputFile = new File(fileName);
// Read the input file
Scanner scanner = new Scanner(inputFile);
while (scanner.hasNext()) {
String word = scanner.next();
wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);
}
scanner.close();
return wordFrequency;
}
public static void writeToFile(HashMap<String, Integer> wordFrequency, String fileName) throws IOException {
FileWriter writer = new FileWriter(fileName);
for (Map.Entry<String, Integer> entry : wordFrequency.entrySet()) {
writer.write(entry.getKey() + ": " + entry.getValue() + "\n");
}
writer.close();
}
}
Explanation:
WordFrequencyCalculator
class contains the main
method where the program execution begins. It sets the input and output file names, and then proceeds to call the calculateWordFrequency
and writeToFile
methods to perform the required operations.calculateWordFrequency
method takes the input file name as a parameter and returns a HashMap<String, Integer>
representing the frequency of each word in the file. It opens the input file using a Scanner
and uses a while
loop to read each word from the file. For each word, it updates the frequency in the wordFrequency
map using the getOrDefault
method to handle both new and existing words efficiently.writeToFile
method takes the word frequency map and the output file name as parameters. It opens the output file using a FileWriter
and uses a for
loop to iterate over each entry in the map. It writes the word and frequency to the output file using the write
method of the FileWriter
.main
method handles potential FileNotFoundException
and IOException
exceptions that may occur during file input/output operations. It catches these exceptions and prints an appropriate error message.Make sure to have the "input.txt" file available in the same directory as the Java program before running the program. The word frequency data will be written to the "output.txt" file.