import java.io.*;
import java.nio.file.*;
import java.util.*;
public class FileModifier {
public static void main(String[] args) {
String folderPath = "path/to/your/folder";
String filenameToRead = "fileToRead.txt";
String filenameWithValues = "valuesFile.txt";
Path filePathToRead = Paths.get(folderPath, filenameToRead);
Path filePathWithValues = Paths.get(folderPath, filenameWithValues);
try {
List<String> valuesToReplace = Files.readAllLines(filePathWithValues);
Set<String> uniqueValues = new HashSet<>(valuesToReplace);
String content = new String(Files.readAllBytes(filePathToRead));
for (String value : uniqueValues) {
content = content.replaceAll(value, "111111");
}
Files.write(filePathToRead, content.getBytes());
System.out.println("File modified successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
public class FileModifier {
public static void main(String[] args) {
String folderPath = "path/to/your/folder";
String filenameWithValues = "valuesFile.txt";
Path folder = Paths.get(folderPath);
Path filePathWithValues = folder.resolve(filenameWithValues);
try {
List<String> lines = Files.readAllLines(filePathWithValues);
Map<String, String> replaceMap = new HashMap<>();
for (String line : lines) {
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
replaceMap.put(parts[0], parts[1]);
}
}
try (Stream<Path> paths = Files.walk(folder)) {
paths.filter(Files::isRegularFile)
.filter(path -> !path.equals(filePathWithValues))
.forEach(filePathToRead -> modifyFile(filePathToRead, replaceMap));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static void modifyFile(Path filePath, Map<String, String> replaceMap) {
try {
String content = new String(Files.readAllBytes(filePath));
for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
content = content.replaceAll(entry.getKey(), entry.getValue());
}
Files.write(filePath, content.getBytes());
System.out.println("File " + filePath.getFileName() + " modified successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.nio.file.*;
import java.util.*;
public class FileModifier {
public static void main(String[] args) {
String folderPath = "path/to/your/folder";
String filenameToRead = "fileToRead.txt";
String filenameWithValues = "valuesFile.txt";
Path filePathToRead = Paths.get(folderPath, filenameToRead);
Path filePathWithValues = Paths.get(folderPath, filenameWithValues);
try {
List<String> lines = Files.readAllLines(filePathWithValues);
Map<String, String> replaceMap = new HashMap<>();
for (String line : lines) {
String[] parts = line.split("\\s+");
if (parts.length >= 2) {
replaceMap.put(parts[0], parts[1]);
}
}
String content = new String(Files.readAllBytes(filePathToRead));
for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
content = content.replaceAll(entry.getKey(), entry.getValue());
}
Files.write(filePathToRead, content.getBytes());
System.out.println("File modified successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}