Transformation values
test
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class TransformationProcessor {
// Method to read the CSV file and store data in a HashMap
public static Map<String, String> readTransformationValues(String csvFilePath) {
Map<String, String> transformationMap = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
transformationMap.put(parts[0].trim(), parts[1].trim());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return transformationMap;
}
// Method to process files in a given directory and replace values based on the HashMap
public static void processDirectory(String directoryPath, Map<String, String> transformationMap) {
File directory = new File(directoryPath);
if (!directory.isDirectory()) {
System.out.println("Provided path is not a directory");
return;
}
File[] files = directory.listFiles((dir, name) -> name.endsWith(".txt")); // Assuming we're working with .txt files
if (files != null) {
for (File file : files) {
processFile(file, transformationMap);
}
}
}
// Method to process a single file
private static void processFile(File file, Map<String, String> transformationMap) {
try {
String content = new String(Files.readAllBytes(file.toPath()));
for (Map.Entry<String, String> entry : transformationMap.entrySet()) {
content = content.replace(entry.getKey(), entry.getValue());
}
// Write the modified content back to the file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java TransformationProcessor <csv_file_path> <directory_path>");
return;
}
String csvFilePath = args[0];
String directoryPath = args[1];
// Read the transformation values from the CSV file
Map<String, String> transformationMap = readTransformationValues(csvFilePath);
// Process the directory to replace values in files
processDirectory(directoryPath, transformationMap);
}
}
test
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
public class TransformationProcessor {
// Method to read the CSV file and store data in a HashMap
public static Map<String, String> readTransformationValues(String csvFilePath) {
Map<String, String> transformationMap = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
transformationMap.put(parts[0].trim(), parts[1].trim());
}
}
} catch (IOException e) {
e.printStackTrace();
}
return transformationMap;
}
// Method to process files in a given directory and replace values based on the HashMap
public static void processDirectory(String directoryPath, Map<String, String> transformationMap) {
File directory = new File(directoryPath);
if (!directory.isDirectory()) {
System.out.println("Provided path is not a directory");
return;
}
processFilesRecursively(directory, transformationMap);
}
// Recursive method to process files in directories and subdirectories
private static void processFilesRecursively(File directory, Map<String, String> transformationMap) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
processFilesRecursively(file, transformationMap);
} else {
processFile(file, transformationMap);
}
}
}
}
// Method to process a single file
private static void processFile(File file, Map<String, String> transformationMap) {
try {
String content = new String(Files.readAllBytes(file.toPath()));
for (Map.Entry<String, String> entry : transformationMap.entrySet()) {
content = content.replace(entry.getKey(), entry.getValue());
}
// Write the modified content back to the file
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java TransformationProcessor <csv_file_path> <directory_path>");
return;
}
String csvFilePath = args[0];
String directoryPath = args[1];
// Read the transformation values from the CSV file
Map<String, String> transformationMap = readTransformationValues(csvFilePath);
// Process the directory to replace values in files
processDirectory(directoryPath, transformationMap);
}
}
test
ete
s
t