Results 1 to 4 of 4
  1. #1
    ninaT is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Help...

    Hi, i hope someone could help me...

    I just want to look in a file for an specific word and when I find it i want to replace that word with some other.

    Any idea on how could i do that? and Can I do that with any type of file,( .txt, .properties, etc)???

    Thanks in advanced

  2. #2
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Try this and let me know if it is what you were looking for. It will accept any filetype and will overwrite the existing file with the new one. I didnt bother testing it much, but it worked with what i had except in notepad the new lines didnt actually make a new line but it looked ok in wordpad and when used as more input.

    Java Code:
      static void replaceWord(String filename, String oldword, String newword) {
    String temp="";
    String line;
        try {
      FileReader input = new FileReader(filename);
     
      BufferedReader bufRead = new BufferedReader(input);
      while ((line=bufRead.readLine())!=null)
      {
     temp += line+"\n";
      }
     temp=temp.replace(oldword, newword);
     System.out.println(temp);
     input.close();
     bufRead = null;
     input = null;
     FileWriter fstream = new FileWriter(filename);
     BufferedWriter out = new BufferedWriter(fstream);
     
     out.write(temp);
     out.close();
    } catch (Exception e) {
      System.out.println("error");
      e.printStackTrace();
    }
      }
    Last edited by Chris.Brown.SPE; 04-12-2008 at 11:11 PM.

  3. #3
    ninaT is offline Member
    Join Date
    Apr 2008
    Posts
    2
    Rep Power
    0

    Default

    Thanks!!! everything worked great!!!!!
    Gracias!!!

  4. #4
    Join Date
    Apr 2008
    Location
    Fort Wayne, Indiana
    Posts
    62
    Rep Power
    0

    Default

    Be sure to mark the thread as solved. And click the rep button for me if you got time. Thanks. Glad i could help.

Posting Permissions

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