Skip to content

Instantly share code, notes, and snippets.

@jerry80409
Created November 19, 2019 02:09
Show Gist options
  • Save jerry80409/b945211532f6f9ae40edb0c5a1af9668 to your computer and use it in GitHub Desktop.
Save jerry80409/b945211532f6f9ae40edb0c5a1af9668 to your computer and use it in GitHub Desktop.
java8_stream_replace_file
/**
* 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 <String> lines = Files.lines(path);
List <String> 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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment