This is the first solution to find and replace a given string with a new string in a file. This approach uses the FileReader, BufferedReader, FileWriter, BufferedWriter, and StringBuffer methods.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ReplaceWordInFile {
public static void main(String[] args) throws IOException {
String lineRead;
/* Display the current sampleFile.txt file contents */
displayFileContent("C:\\Users\\Nagendra\\sampleFile.txt","old");
/* Re-open the same sampleFile.txt file using BufferedReader and FileReader,
to search for a given word in each line of this file */
FileReader fileRead=new FileReader("C:\\Users\\Nagendra\\sampleFile.txt");
@SuppressWarnings("resource")
BufferedReader fileToRead = new BufferedReader(fileRead);
System.out.println();
Scanner inpStr=new Scanner(System.in);
System.out.print("Enter the String to be searched in the given file: ");
String strToSrch=inpStr.next();
System.out.print("Enter the String to be replaced in the given file: ");
String strToReplace=inpStr.next();
System.out.println("\n");
/* Open the new sampleFileUpdated.txt file using BufferedWriter and FileWriter */
FileWriter fWrite=new FileWriter("C:\\Users\\Nagendra\\SampleFileUpdated.txt");
BufferedWriter fileToWrite = new BufferedWriter(fWrite);
StringBuffer lineReadBuff= new StringBuffer();
/* Read line by line and search for the given word in each line of sampleFile.txt
* using the string buffer methods. Call the function parseEachLine() */
while ((lineRead= fileToRead.readLine()) != null) {
lineReadBuff.append(lineRead);
/*Write the new line into the sampleFileUpdated.txt file after
replacing with a given word in each line of sampleFile.txt file*/
fileToWrite.write(parseEachLine(lineReadBuff,strToSrch,strToReplace));
lineReadBuff.delete(0, lineReadBuff.length());
}
fileToWrite.close();
/* Display the newly created sampleFileUpdated.txt file contents */
displayFileContent("C:\\Users\\Nagendra\\SampleFileUpdated.txt","new");
inpStr.close();
}
private static String parseEachLine(StringBuffer lineRd,String strSrch,String strReplace){
int startIndex=0,foundIndex=0;
/* Search for the give word in each line multiple times
with the help of the indexOf method */
while (foundIndex != -1) {
foundIndex=lineRd.indexOf(strSrch,startIndex);
if (foundIndex != -1) {
lineRd.delete(foundIndex, foundIndex+strSrch.length());
lineRd.insert(foundIndex, strReplace);
startIndex=foundIndex+strReplace.length();
}
}
return lineRd.toString()+"\n";
}
private static void displayFileContent(String filePath, String neworold) throws IOException {
/* Display the sample file contents to the console using the
BufferedReader and FileReader */
FileReader fRead=new FileReader(filePath);
@SuppressWarnings("resource")
BufferedReader fileToRd = new BufferedReader(fRead);
String lineRead;
System.out.println("The contents of the "+neworold+" file are below");
System.out.println("----------------------------------");
while ((lineRead= fileToRd.readLine()) != null)
System.out.println(lineRead);
fileToRd.close();
}
}
OUTPUT:
The contents of the old file are below
----------------------------------
This is line one with language word available.
The second line has no such word.
Third line has language word by default.
language and language repeated twice in the fourth line.
Enter the String to be searched in the given file: language
Enter the String to be replaced in the given file: java
The contents of the new file are below
----------------------------------
This is line one with java word available.
The second line has no such word.
Third line has java word by default.
java and java repeated twice in the fourth line.

Leave a comment