Click here to Skip to main content
Licence 
First Posted 30 Oct 2001
Views 166,274
Bookmarked 24 times

Searching and replacing strings in a text file

By | 30 Oct 2001 | Article
A simple Java toy program for searching and replacing strings in a text file.

Introduction

At work, I was asked to write a command line utility program that will open a text file, search for a list of strings, and replace them with a list of new strings. The catch is, the program has to work on all platforms within my company (mostly, NT, Win2K, and AIX ). Since I am not familiar with Perl or other scripting languages, Java seemed to be my only choice.

TextRep, the java class I wrote for this purpose, first opens the input file and makes a backup copy of it. Then it starts to search and replace the strings specified by the user. Here are some examples on how to use this little program:

java TextRep c:\temp\test1.txt "Hello, world" "Hello, java world" 
java TextRep c:\temp\test1.txt "Hello, java world" "Hello, world" 
java TextRep c:\temp\test2.txt "server" "client"

The first command replaces all instances of the string "Hello, world" in file c:\temp\test1.txt with the string "Hello, java world". The second command will undo the changes made by the first command. The third command will replace all instances of the string "server" in file c:\temp\test2.txt with the string "client". If you want to replace the strings "word1", "word2", "word3", ..., with the strings "replace1", "replace2", "replace3", ..., respectively, in file mytest.txt, all you need to do is create a file, say mystrings.txt, containing the following lines:

word1 
replace1 
word2 
replace2 
word3 
replace3 
...

and run the following command:

java TextRep mytest.txt mystrings.txt

The source code included with this article is straightforward (with comments), but apparently not the most efficient Java code. You can probably find other free programs to do the same thing (or more) on multiple platforms. I hope someone will find my program useful. Thanks for reading.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Xiangyang Liu 刘向阳



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionThank you very much Pinmembersanya.fesak19:02 21 Sep '11  
GeneralThank you PinmemberMember 78770843:46 27 Apr '11  
GeneralFew Code Issues Pinmemberprgrmmr7367:56 26 May '09  
QuestionHelp with Search and replace in a file Pinmembergo4rib20:07 24 Oct '08  
GeneralWord Search COncept Pinmembercoolnaveen123421:52 19 Apr '06  
GeneralXiang Liu Pinmemberweathers18:06 26 Jan '03  
GeneralRe: Xiang Liu PinmemberXiangyang Liu3:09 27 Jan '03  
GeneralUse 'sed' instead. PinmemberAnonymous7:25 5 Nov '01  
GeneralRe: Use 'sed' instead. PinmemberJon Sagara7:30 5 Nov '01  
GeneralRe: Use 'sed' instead. PinmemberXiangYangLiu21:35 5 Nov '01  
GeneralRe: Use 'sed' instead. PinmemberMember 78770843:44 27 Apr '11  
GeneralRe: Use 'sed' instead. PinmemberXiangYangLiu15:57 5 Nov '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 31 Oct 2001
Article Copyright 2001 by Xiangyang Liu 刘向阳
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid