Java 8- How to replace word in a File using Stream
On this page, you will learn how to find and replace words in a file using Java Stream. Java introduced Stream
interface under the java.util.stream
package in the JDK 8 version.
Here we have a simple text file named info.txt which contains some information about the user.
Hi, My name is Atul Rai and my favorite color is Blue.
I have completed my master's in 2014.
And you want to replace the user information like favorite color Blue with Black and master’s year 2014 with 2016.
Step 1: Create a method which the contain the combination of old word and the new word.
public static Map<String, String> replaceWith() {
Map<String, String> map = new HashMap<>();
map.put("Blue", "Black");
map.put("2014", "2016");
return map;
}
Step 2: Create another method which loops the Map
key, replace with its value and assign to the String
.
public static String replaceWords(String str, Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (str.contains(entry.getKey())) {
str = str.replace(entry.getKey(), entry.getValue());
}
}
return str;
}
Step 3: Read the file location, instantiate the Stream and call these methods in the main method.
Map<String, String> m = replaceWith();
Path filePath = Paths.get("F:\\text\\info.txt");
Stream<String> lines = Files.lines(filePath, Charset.forName("UTF-8"));
List<String> replacedLine = lines.map(line -> replaceWords(line, m)).collect(Collectors.toList());
Files.write(filePath, replacedLine, Charset.forName("UTF-8"));
lines.close();
Let’s see the complete example.
package org.websparrow;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamReplaceWordtDemo {
public static void main(String[] args) {
Map<String, String> m = replaceWith();
Path filePath = Paths.get("F:\\text\\info.txt");
try {
Stream<String> lines = Files.lines(filePath, Charset.forName("UTF-8"));
List<String> replacedLine = lines.map(line -> replaceWords(line, m)).collect(Collectors.toList());
Files.write(filePath, replacedLine, Charset.forName("UTF-8"));
lines.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, String> replaceWith() {
Map<String, String> map = new HashMap<>();
map.put("Blue", "Black");
map.put("2014", "2016");
return map;
}
public static String replaceWords(String str, Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (str.contains(entry.getKey())) {
str = str.replace(entry.getKey(), entry.getValue());
}
}
return str;
}
}
Output: Now you can see that old words replaced with new words.
Hi, My name is Atul Rai and my favorite color is Black.
I have completed my master's in 2016.
If you want to store the modified user information into a new file. Create a new file path object and pass it to Files.write()
method.
Path newFilePath = Paths.get("F:\\text\\new_info.txt");
Files.write(newFilePath, replacedLine, Charset.forName("UTF-8"));