Text File Modification Plan :

Write a coffee plan or role to supervene upon specific string in text file. Your plan should accept one text file every bit input and supercede a specific string in that text file with new cord.

How To Supercede Specific String In Text File In Java?

We are defining ane method calledmodifyFile().This method reads all the content of input text file into a Cord object, replaces the onetime cord with new string and rewrites the new content dorsum into the same file. It takes three arguments : Path of the file to exist modified – filepath, cord to be replaced – oldString and cord to be replaced with – newString. Let's see how this method works step by pace.

Step 1 : Create a File object by passing the path of the file to be modified.

File fileToBeModified = new File(filePath)

Footstep 2 : InitializeoldContent with an empty string. This String object will hold all the old content of the input text file.

String oldContent = ""

Step 3 : CreateBufferedReader object to read the input text file line by line.

BufferedReader reader = new BufferedReader(new FileReader(fileToBeModified))

Step 4 : Read all the lines of input text file one past 1and append information technology tooldContent.

String line = reader.readLine();

while (line != null)
{
        oldContent = oldContent + line + System.lineSeparator();
        line = reader.readLine();
}

Pace v : Replace all the occurrences of oldString with newString using replaceAll() method. You will get a newContent.

String newContent = oldContent.replaceAll(oldString, newString)

Step 6 : Now createFileWriter object to write newContent back into the input text filefileToBeModified.

FileWriter writer = new FileWriter(fileToBeModified)

Step vii : Rewrite the fileToBeModifiedwithnewContentusing write() method ofFileWriterobject.

writer.write(newContent)

Step viii : Close the resources.

reader.close()
writer.close()

Java Program To Supervene upon Specific String In Text File :

The beneath program takes StudentFile.txt as input and replaces the marks 85 with 95.

how to replace specific string in text file in java modify text file in java

import coffee.io.BufferedReader; import java.io.File; import java.io.FileReader; import coffee.io.FileWriter; import coffee.io.IOException;  public grade TextFileModificationProgram {	 	static void modifyFile(Cord filePath, Cord oldString, String newString) 	{ 		File fileToBeModified = new File(filePath); 		 		String oldContent = ""; 		 		BufferedReader reader = naught; 		 		FileWriter writer = zip; 		 		attempt  		{ 			reader = new BufferedReader(new FileReader(fileToBeModified)); 			 			//Reading all the lines of input text file into oldContent 			 			String line = reader.readLine(); 			 			while (line != nothing)  			{ 				oldContent = oldContent + line + System.lineSeparator(); 				 				line = reader.readLine(); 			} 			 			//Replacing oldString with newString in the oldContent 			 			String newContent = oldContent.replaceAll(oldString, newString); 			 			//Rewriting the input text file with newContent 			 			writer = new FileWriter(fileToBeModified); 			 			author.write(newContent); 		} 		catch (IOException eastward) 		{ 			e.printStackTrace(); 		} 		finally 		{ 			try  			{ 				//Closing the resources 				 				reader.close(); 				 				writer.close(); 			}  			take hold of (IOException e)  			{ 				east.printStackTrace(); 			} 		} 	} 	 	public static void main(String[] args) 	{ 		modifyFile("C:/StudentFile.txt", "85", "95"); 		 		Organisation.out.println("washed"); 	} }                

StudentFile.txt (Earlier Modification) :

Students	Marks ====================== Abhi		85 Bhavani		55 Mahesh		67 Nalini		85 Shloka		48 Kaya		85 Siya		58 Vikas		85                

StudentFile.txt (Afterward Modification) :

Students	Marks ====================== Abhi		95 Bhavani		55 Mahesh		67 Nalini		95 Shloka		48 Kaya		95 Siya		58 Vikas		95                

Also Read :

How to sort text file in coffee?

How to compare 2 text files in java?

How to detect about repeated word in a text file in coffee?

How to listing all files in a directory in java?

How to read and write images in java?