Recursive Word Replacement

In this project you will write a recursive function to replace an old string with a new string in all files in a directory and its subdirectories.

Download the code and the zip file. You should implement the method replaceWord (with four parameters) in (leave the other functions as they are)

The method gets four parameters, file, oldString, newString, and dirFileChanged:
- file: a File object initialized with the path to a file or directory,
- oldString: a String to be replaced by newString.
- dirFileChanged: an int array of length 3. After the original call to the method is executed completely, the first element has the number of directories (and subdirectories) that were checked by the method during its run, the second element is the number of files that were checked and the third element includes the number of files that were updated (oldString has occurred and replaced in the file at least once).

If you unzip the included zip file in your project's src directory, and run the program with a correct implementation you will see the following output:

Checked 0 directory, and updated 0 out of 0 file.
Checked 0 directory, and updated 1 out of 1 file.
Checked 3 directories, and updated 1 out of 3 files.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

public class Recursive {

public static void main(String[] args) {
    String dir = System.getProperty("user.dir") +
      System.getProperty("file.separator") + "src" +
      System.getProperty("file.separator") + "dir";
    replaceWord(new File("***someFileThatDoesn'tExist***"), "I", "you");
    replaceWord(new File(dir + System.getProperty("file.separator") + "file1.txt"), "I", "you");
    replaceWord(new File(dir), "I", "you");

public static void replaceWord(File file, String oldString, String newString) {
    int[] dirFileChanged = new int[3];
    replaceWord(file, oldString, newString, dirFileChanged);
    System.out.println("Checked " + dirFileChanged[0] +
                      (dirFileChanged[0] <= 1? " directory,": " directories,") +
                      " and updated " + dirFileChanged[2] + " out of " + dirFileChanged[1] +
                      (dirFileChanged[1] <= 1? " file.": " files...

By purchasing this solution you'll be able to access the following files:

for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats