Results 1 to 8 of 8
Thread: Search and Replace
- 02-28-2012, 11:12 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Search and Replace
Hi Everyone,
I'm writing here because I am new to java and I still not know very well all the framework.
I'm look for making a class method that allow me to "search&replace" text into a file.
Mainly I would like to give to this method 3 input:
- A string with the path to the file (es /home/user/file.txt)
- A string to find (es dog)
- A string to repliche (es cat)
And I would like that all the occurences of "dog"s in the file.txt will be replaced with "cat"s.
I have tryed with "pattern" & "macher" in the java.util.regex.* but I don't understand very well how it works.
Could someone help me please?!?! :(
- 02-28-2012, 11:24 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Search and Replace
Since you've tried something then you might want to show us that code, and explain where it doesn't work.
That will give us an idea of how far forward you've got, saving us some time (potentially).Please do not ask for code as refusal often offends.
- 02-28-2012, 11:36 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Re: Search and Replace
Ok. I have tryed with this code but at the end I have a file with every string repeted 3 times and replaced "alternatively"
For example. I have to replace dog->cat , lion -> leopard and apple-> banana,
I have this:
cat, lion, apple
dog, leopard, apple
dog, lion, banana
:(
This is the method:
Java Code:public static void readLineByLine(String destFolder,int processor) throws Exception { // First copy the file into the directory //BSPXCPAutoConfig.copyfile(CURRENT_DIRECTORY+"/XCP_Template_CProject", destFolder+"/cproject"); // Read the file and replace text File f = new File(CURRENT_DIRECTORY+"/XCP_Template_CProject"); FileWriter file = new FileWriter (destFolder+"/cproject"); PrintWriter printer = new PrintWriter(file); //Matcher folerMatcher = n if (!f.exists() && f.length() < 0) { System.out.println("The specified file does not exist"); } else { FileReader fr = new FileReader(f); BufferedReader reader = new BufferedReader(fr); String st = ""; String replace; if (processor==0) replace = "host_if_mb"; else replace = "mb_"+processor; while ((st = reader.readLine()) != null) { Pattern p = Pattern.compile("##PROCESSOR_NAME##"); Matcher m = p.matcher(st); printer.println(m.replaceAll(replace)); // ---------- replace = "empty_cpp_bsp_"+processor; p = Pattern.compile("##CPP_BSP_FOLDER##"); m = p.matcher(st); printer.println(m.replaceAll(replace)); // ---------- replace = "BLA BLA"; p = Pattern.compile("##PROJECTFOLDER##"); // get a matcher object m = p.matcher(st); printer.println(m.replaceAll(replace)); } } }
- 02-28-2012, 12:08 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Search and Replace
You are replacing and printing 3 times in your while loop.
You should create your string with everything replaced first, before printing it.
Java Code:while(blah) { ... create first Matcher using 'st' ... String current = m.replaceAll(replace); .. new Matcher using 'current'... current = m.replaceAll(replace); ...etc etc... printer.println(current); }Please do not ask for code as refusal often offends.
- 02-28-2012, 12:33 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Re: Search and Replace
Aaaah, ok!!! I'll try immediately! Thank you! I will let you know asap! Thank You!!! :)
- 02-28-2012, 01:18 PM #6
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
Re: Search and Replace
Yeeesss !!! It woks!!! Thank You!!! :D :D :D :D
- 02-28-2012, 04:15 PM #7
Member
- Join Date
- Feb 2012
- Location
- Delhi, India
- Posts
- 5
- Rep Power
- 0
Re: Search and Replace
Can anyone explain me wat these different processors name and their meaning ?
- 02-28-2012, 04:33 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Any advice - java search algorithms which accept multiple search parameters
By Alfster in forum New To JavaReplies: 4Last Post: 03-24-2011, 11:50 PM -
How to Search, Compare, and Replace text in the TextBox?
By SHENGTON in forum CLDC and MIDPReplies: 4Last Post: 01-20-2011, 04:19 PM -
Regex search and replace
By joe robles in forum Advanced JavaReplies: 1Last Post: 09-25-2010, 02:32 PM -
make search function ike eclipse search in window->preference
By i4ba1 in forum Advanced JavaReplies: 5Last Post: 08-26-2008, 03:43 PM -
How to search and replace a tag value in xml file using java
By karthik84 in forum XMLReplies: 2Last Post: 08-18-2008, 02:59 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks