Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Tolls

Thread: Search and Replace

  1. #1
    AtoN is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Unhappy 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?!?! :(

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

  3. #3
    AtoN is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default 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));
                }
        }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    AtoN is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Re: Search and Replace

    Aaaah, ok!!! I'll try immediately! Thank you! I will let you know asap! Thank You!!! :)

  6. #6
    AtoN is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Re: Search and Replace

    Yeeesss !!! It woks!!! Thank You!!! :D :D :D :D

  7. #7
    Join Date
    Feb 2012
    Location
    Delhi, India
    Posts
    5
    Rep Power
    0

    Default Re: Search and Replace

    Can anyone explain me wat these different processors name and their meaning ?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Search and Replace

    What processor name?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 03-24-2011, 11:50 PM
  2. Replies: 4
    Last Post: 01-20-2011, 04:19 PM
  3. Regex search and replace
    By joe robles in forum Advanced Java
    Replies: 1
    Last Post: 09-25-2010, 02:32 PM
  4. Replies: 5
    Last Post: 08-26-2008, 03:43 PM
  5. Replies: 2
    Last Post: 08-18-2008, 02:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •