/** * Ref: https://www.technicalkeeda.com/java-8-tutorials/java-8-stream-find-and-replace-file-content */ public static void main(String[] args) { try { Path path = Paths.get("c:\\demo.txt"); Stream lines = Files.lines(path); List replaced = lines.map(line -> line.replaceAll("foo", "bar")).collect(Collectors.toList()); Files.write(path, replaced); lines.close(); System.out.println("Find and Replace done!!!"); } catch (IOException e) { e.printStackTrace(); } }