IOUtil { public static void fileFindReplace(File file, String find, String replace) throws IOException { String fileContents = FileUtils.readFileToString(file); fileContents = fileContents.replaceAll("\\Q" + find + "\\E", replace); FileUtils.writeStringToFile(file, fileContents); } private  IOUtil(); static String readStringFromStream(InputStream stream); static InputStream writeStringToStream(String string); static OutputStream getOutputStream(File file); static void copy(InputStream input, OutputStream output); static void copyFileToDirectory(File file, File dir); static void fileFindReplace(File file, String find, String replace); static File writeStreamToFile(InputStream inStream); static File writeStreamToFile(InputStream inStream, boolean gzip); static InputStream getFileStream(File file); static void addFileToZipOutputStream(File file, ZipOutputStream zipOs);  }
@Test public void testFileFindReplace() throws Exception { File testFile = File.createTempFile("temp", "file"); try { String fileContent = "before $TO_REPLACE after"; FileUtils.writeStringToFile(testFile, fileContent); IOUtil.fileFindReplace(testFile, "$TO_REPLACE", "and"); String finalContent = FileUtils.readFileToString(testFile); assertEquals("before and after", finalContent); } finally { testFile.delete(); } }