import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class StagingDataImporter {
/**
* Imports data from a file, treating each line as a separate record.
*
* @param filePath The path to the file containing the data.
* @return A list of strings, where each string represents a line from the file.
* Returns an empty list if the file is not found or an error occurs.
*/
public static List<String> importData(String filePath) {
List<String> data = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
data.add(line); // Add each line to the list
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
return data; // Return empty list on error
}
return data;
}
public static void main(String[] args) {
// Example Usage
String filePath = "staging_data.txt"; // Replace with your file path
List<String> importedData = importData(filePath);
if (importedData != null) {
for (String line : importedData) {
System.out.println(line); // Print the imported data
}
} else {
System.out.println("No data imported.");
}
}
}
Add your comment