code: public void replaceStringInFile(File folder, File file, String template, String[] replacement)
              throws IOException {
          System.out.println("Patching file: "+file.getCanonicalPath());
          String name = file.getName();
          File origFile = new File(folder, name+".orig");
          file.renameTo(origFile);
          File temporaryFile = new File(folder, name+".tmp");
          if (temporaryFile.exists()) {
              temporaryFile.delete();
          }
          temporaryFile.createNewFile();
          String line;
          FileInputStream fis = new FileInputStream(origFile);
          PrintWriter pw = new PrintWriter(temporaryFile);
          BufferedReader br = new BufferedReader(new InputStreamReader(fis));
          while ((line = br.readLine()) != null) {
              if (line.trim().equals(template)) {
                  for (String s : replacement) {
                      pw.println(s);
                  }
              } else {
                  pw.println(line);
              }
          }
          pw.flush();
          pw.close();
          if (!temporaryFile.renameTo(new File(folder, name))) {
              throw new IOException("Unable to rename file in folder "+folder.getName()+
                      " from \""+temporaryFile.getName()+"\" into \""+name +
                      "\n Original file saved as "+origFile.getName());
          }
          origFile.delete();
      }
docs: /*
  Replace one line in the given file in the given folder with the lines given
  @param folder Folder in which file should be created
  @param file Original file to patch
  @param template Trimmed String with the pattern we are have to find
  @param replacement Array of String that has to be written in the place of first line matching the template