Program to Replace all the occurrences of searchWord in the file with replaceWord in java

Let’s say c:/myFile.txt looks like this >
abc is the string abc this is abc

1) Read file in String (i.e. in fileDataInString).
2) Replace all the occurrences of searchWord in the file (i.e. in fileDataInString) with replaceWord by using replaceAll method of String.
3) Now, write fileDataInString in file.



Program to Replace all the occurrences of searchWord in the file with replaceWord in java >

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ReplaceAllOccurrencesOfStringInFile {
   public static void main(String[] args) {
         
          String fileName ="c:/myFile.txt";
          String searchWord= "abc";
          String replaceWord= "xyz";
         
          String fileDataInString = new String();
         
          try {
                 //1) Read file in String (i.e. in fileDataInString).
                 FileInputStream fis = new FileInputStream(fileName);
                 BufferedInputStream bis = new BufferedInputStream(fis, 10000);
                 int data;
                 while((data = bis.read())!=-1){
                       fileDataInString = fileDataInString+String.valueOf(((char)data));
                 }
                 bis.close();
                
                 //By now, String fileDataInString contains all the data of file
                
                
                 //2) Replace all the occurrences of searchWord in the file (i.e. in
                 //fileDataInString)with replaceWord by using replaceAll method of String.
                 fileDataInString = fileDataInString.replaceAll(searchWord, replaceWord);
                
                 //3) Now, write fileDataInString in file.
                 FileOutputStream fos = new FileOutputStream(fileName);
                 BufferedOutputStream bos = new BufferedOutputStream(fos);
                 bos.write(fileDataInString.getBytes());
                 bos.close();
                
                 System.out.println("all occurrences of "+searchWord+" has been replaced with "
                              +replaceWord+" in "+fileName);
                
          } catch (FileNotFoundException e) {
                 e.printStackTrace();
          } catch (IOException e) {
                 e.printStackTrace();
          }
         
   }
}
/*OUTPUT
all occurrences of abc has been replaced with xyz in c:/myFile.txt
*/



After executing above program all occurrences of abc will be replaced with xyz in c:/myFile.txt, and c:/myFile.txt will look like this >
xyz is the string xyz this is xyz




RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways


Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO


Find current directory or current path in java


eEdit
Must read for you :