Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prompt the user to enter the path and name of a text file for input. Open this f

ID: 3564435 • Letter: P

Question

Prompt the user to enter the path and name of a text file for input.

Open this file for input in your program.

Present the user with the following options:

Count and print the total number of characters in the file.

Count and print the total number of words in the file.

Count and print the total number of occurrences of a user- specified word in the file.

Search for a given word in a file and change all of its occurrences to another word specified by the user.

Save the altered text to a new file. Allow the user to specify the name and location of the new text file

Explanation / Answer

// Do let me know, if you have any issues

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;


public class FileOperations {

   public static void main(String[] args) throws IOException {

       Scanner keyboard = new Scanner(System.in);
       boolean filePathValid;
       File file;
       do{
           System.out.print("Enter complete filename with path>> ");
           String pathName = keyboard.nextLine();

           file = new File(pathName);
           if(file.exists())
               filePathValid = true;
           else{
               filePathValid = false;
               System.out.println("File Not found, try again");
           }
       }
       while(!filePathValid);

       int option;

       do{
           System.out.println("1. Get total number of characters in file");
           System.out.println("2. Get total number of words in file");
           System.out.println("3. Get count of a specific word in file");
           System.out.println("4. Replace a word with another and save new file");
           System.out.println("5. Exit");

           System.out.print(" Enter your Choice>> ");
           option = keyboard.nextInt();
          
           Scanner scanReader;
           BufferedReader reader;
           switch (option) {
           case 1:
               reader = new BufferedReader(new FileReader(file));
               int charCount = 0;
               while(reader.read()!=-1){
                   charCount++;
               }
               System.out.println(" Total no of characters: "+charCount);
               reader.close();
               break;
           case 2:
               scanReader = new Scanner(file);
               int wordCount = 0;
               while(scanReader.hasNext()){
                   String word = scanReader.next();
                   if(word.indexOf("\")==-1) // skips special characters
                       wordCount++;
               }
               System.out.println("Number of words: "+wordCount);
               scanReader.close();
               break;
           case 3:
               System.out.print("Enter Word>>");
               String inWord = keyboard.next();
               scanReader = new Scanner(file);
               int count = 0;
               while(scanReader.hasNext()){
                   String word = scanReader.next();
                   if(word.equalsIgnoreCase(inWord))
                       count++;
               }
               System.out.println("Count of "+inWord+" is : "+count);
               scanReader.close();
               break;
           case 4:
               System.out.print("Enter word of file>>");
               String fileWord = keyboard.next();
               System.out.print("Enter new word>>");
               String replacement = keyboard.next();
               StringBuilder fileContent = new StringBuilder();
               reader = new BufferedReader(new FileReader(file));
               String line;
               while((line=reader.readLine())!=null){
                   fileContent.append(line);
               }
              
               String newContent = fileContent.toString().replace(fileWord, replacement);
              
               System.out.print("Enter new file name with path>>");
               String newFilePath = keyboard.next();
               File newFile = new File(newFilePath);
               newFile.createNewFile();
               FileWriter writer = new FileWriter(newFile);
               writer.write(newContent);
               writer.flush();
               writer.close();
               System.out.println("New File Written: "+newFile.toString());
               break;

           default:
               System.out.println("Invalid Option");
               break;
           }


       }
       while(option!=5);
       keyboard.close(); // close stream

   }

}

_______________________________________________________

output

Enter complete filename with path>> report.txt
1. Get total number of characters in file
2. Get total number of words in file
3. Get count of a specific word in file
4. Replace a word with another and save new file
5. Exit

Enter your Choice>> 1

Total no of characters: 43
1. Get total number of characters in file
2. Get total number of words in file
3. Get count of a specific word in file
4. Replace a word with another and save new file
5. Exit

Enter your Choice>> 2
Number of words: 10
1. Get total number of characters in file
2. Get total number of words in file
3. Get count of a specific word in file
4. Replace a word with another and save new file
5. Exit

Enter your Choice>> 3
Enter Word>>count
Count of count is : 2
1. Get total number of characters in file
2. Get total number of words in file
3. Get count of a specific word in file
4. Replace a word with another and save new file
5. Exit

Enter your Choice>> 4
Enter word of file>>count
Enter new word>>hello
Enter new file name with path>>newTest.txt
New File Written: newTest.txt
1. Get total number of characters in file
2. Get total number of words in file
3. Get count of a specific word in file
4. Replace a word with another and save new file
5. Exit

Enter your Choice>> 5