1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class TextFileMirror {
  5. public static void mirrorTextFile(String inputFilePath, String outputFilePath) throws IOException {
  6. // Read data from the input file
  7. List<String> lines = readLinesFromFile(inputFilePath);
  8. // Write the data to the output file
  9. writeLinesToFile(outputFilePath, lines);
  10. }
  11. private static List<String> readLinesFromFile(String filePath) throws IOException {
  12. List<String> lines = new ArrayList<>();
  13. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  14. String line;
  15. while ((line = reader.readLine()) != null) {
  16. lines.add(line);
  17. }
  18. }
  19. return lines;
  20. }
  21. private static void writeLinesToFile(String filePath, List<String> lines) throws IOException {
  22. try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
  23. for (String line : lines) {
  24. writer.write(line);
  25. writer.newLine(); // Add a new line after each line from the input file
  26. }
  27. }
  28. }
  29. public static void main(String[] args) {
  30. if (args.length != 2) {
  31. System.out.println("Usage: java TextFileMirror <input_file_path> <output_file_path>");
  32. return;
  33. }
  34. String inputFilePath = args[0];
  35. String outputFilePath = args[1];
  36. try {
  37. mirrorTextFile(inputFilePath, outputFilePath);
  38. System.out.println("File mirrored successfully.");
  39. } catch (IOException e) {
  40. System.err.println("Error mirroring file: " + e.getMessage());
  41. }
  42. }
  43. }

Add your comment