Saturday, August 8, 2009

Replacing String In File


Hi Reader,

It's going to be my second post.
This post is not as a rocket science but needful.
I got idea to create this program, when i got issue with my module.
There was an xml document and i need to know the occurances of a particular string.
The file size was more than 10 mb. I used edit plus for this. First it took time for loading file in edit plus and then when i tried hit "ctrl + h" (to find and replacce dialog box) duhh! Got hanged.
Then i used my program. And felt glad.

Please have look may be it ll be helpful for you also.

This program replaces all the occurances of a given string to be replaced in the given file with the given string to be replaced by.

  1.   package src;  
  2.   import java.io.BufferedReader;  
  3.   import java.io.BufferedWriter;  
  4.   import java.io.File;  
  5.   import java.io.FileReader;  
  6.   import java.io.FileWriter;  
  7.   import java.io.IOException;  
  8.   
  9.   public class ReplaceDataInFile {  
  10.      public static void main(String args[]) {  
  11.      ReplaceDataInFile ob = new ReplaceDataInFile();  
  12.      ob.replaceStringInFile("C:\\_T_Work""file.txt""old""new");  
  13.   }  
  14.   
  15.   
  16. <div align="left">  private void replaceStringInFile(final String fileLoc, final String fileName, final String toReplace, final String replaceBy) {  
  17.   
  18.     File file = null;  
  19.     BufferedReader br = null;   
  20.     BufferedWriter bw = null;  
  21.     String currentLine = "";  
  22.     String oldContentOfFile = "";  
  23.     int occuranceFound = 0;  
  24.     int countLine = 0;</div><div align="left">  
  25.   
  26.     try {  
  27.       file = new File(fileLoc + System.getProperty("file.separator") + fileName);  
  28.       br = new BufferedReader(new FileReader(file));  
  29.         
  30.       while ((currentLine = br.readLine()) != null) {  
  31.         countLine++;  
  32.         oldContentOfFile = oldContentOfFile + currentLine + "\r\n";  
  33.         System.out.println("Line " + countLine + " : " + currentLine);  
  34.   
  35.         if (currentLine.contains(toReplace)) {  
  36.           occuranceFound++;  
  37.         }  
  38.       }</div><div align="left">  
  39.     } catch (IOException e) {  
  40.          System.out.println("Error Occured When Reading File : " + e);  
  41.          System.exit(1);  
  42.     } finally {  
  43.          if (br != null) {  
  44.            try {  
  45.              br.close();  
  46.            } catch (IOException e) {  
  47.                System.out.println("Error Occured When Closing Reader : " + e);  
  48.                System.exit(1);  
  49.            }  
  50.          }  
  51.      }</div><div align="left">  
  52.   
  53.      System.out.println();  
  54.      System.out.println("Old Content of The File : ");  
  55.      System.out.println(oldContentOfFile);  
  56.   
  57.      String latestText = oldContentOfFile.replaceAll(toReplace, replaceBy);  
  58.      System.out.println("Latest Content of The File : ");  
  59.      System.out.println(latestText);</div><div align="left">  
  60.   
  61.      try {  
  62.      bw = new BufferedWriter(new FileWriter(fileLoc + System.getProperty("file.separator") + fileName));  
  63.      bw.write(latestText);  
  64.      System.out.println("Total [" + occuranceFound + "] Occurances of ["  
  65. + toReplace + "] Replaced By [" + replaceBy + "].");  
  66.      } catch (IOException e) {  
  67.          System.out.println("Error Occured When Writtin To File : " + e);  
  68.      } finally {  
  69.         if (bw != null) {  
  70.           try {  
  71.             bw.close();  
  72.           } catch (IOException e) {  
  73.               System.out.println("Error Occured When Closing Writer : " + e);  
  74.               System.exit(1);  
  75.           }  
  76.         }  
  77.     }  
  78.   }  
  79. }</div>  



To execute this just change the paramter of the method called in main().

Waiting for your responses.

Thanks,
Tanzy.