1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class StagingDataImporter {
  7. /**
  8. * Imports data from a file, treating each line as a separate record.
  9. *
  10. * @param filePath The path to the file containing the data.
  11. * @return A list of strings, where each string represents a line from the file.
  12. * Returns an empty list if the file is not found or an error occurs.
  13. */
  14. public static List<String> importData(String filePath) {
  15. List<String> data = new ArrayList<>();
  16. try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
  17. String line;
  18. while ((line = reader.readLine()) != null) {
  19. data.add(line); // Add each line to the list
  20. }
  21. } catch (IOException e) {
  22. System.err.println("Error reading file: " + e.getMessage());
  23. return data; // Return empty list on error
  24. }
  25. return data;
  26. }
  27. public static void main(String[] args) {
  28. // Example Usage
  29. String filePath = "staging_data.txt"; // Replace with your file path
  30. List<String> importedData = importData(filePath);
  31. if (importedData != null) {
  32. for (String line : importedData) {
  33. System.out.println(line); // Print the imported data
  34. }
  35. } else {
  36. System.out.println("No data imported.");
  37. }
  38. }
  39. }

Add your comment