import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class TextFileMirror {
public static void mirrorTextFile(String inputFilePath, String outputFilePath) throws IOException {
// Read data from the input file
List<String> lines = readLinesFromFile(inputFilePath);
// Write the data to the output file
writeLinesToFile(outputFilePath, lines);
}
private static List<String> readLinesFromFile(String filePath) throws IOException {
List<String> lines = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
}
return lines;
}
private static void writeLinesToFile(String filePath, List<String> lines) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
for (String line : lines) {
writer.write(line);
writer.newLine(); // Add a new line after each line from the input file
}
}
}
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java TextFileMirror <input_file_path> <output_file_path>");
return;
}
String inputFilePath = args[0];
String outputFilePath = args[1];
try {
mirrorTextFile(inputFilePath, outputFilePath);
System.out.println("File mirrored successfully.");
} catch (IOException e) {
System.err.println("Error mirroring file: " + e.getMessage());
}
}
}
Add your comment