Created
November 19, 2019 02:09
-
-
Save jerry80409/b945211532f6f9ae40edb0c5a1af9668 to your computer and use it in GitHub Desktop.
java8_stream_replace_file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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