1. import java.io.*;
  2. import java.util.Properties;
  3. public class DataTruncator {
  4. private static final String CONFIG_FILE = "config.properties";
  5. private static int maxRecordLength = 0;
  6. public static void main(String[] args) {
  7. try {
  8. loadConfiguration();
  9. truncateData();
  10. } catch (IOException e) {
  11. System.err.println("Error: " + e.getMessage());
  12. }
  13. }
  14. private static void loadConfiguration() throws IOException {
  15. Properties props = new Properties();
  16. try (FileInputStream fis = new FileInputStream(CONFIG_FILE)) {
  17. props.load(fis);
  18. maxRecordLength = Integer.parseInt(props.getProperty("maxRecordLength"));
  19. }
  20. }
  21. private static void truncateData() throws IOException {
  22. // Simulate reading data from a source
  23. String[] data = {"This is a very long string that needs to be truncated.",
  24. "Another long string for testing the truncation.",
  25. "Short string"};
  26. for (String record : data) {
  27. if (record.length() > maxRecordLength) {
  28. String truncatedRecord = record.substring(0, maxRecordLength) + "..."; // Truncate and add ellipsis
  29. System.out.println("Original: " + record);
  30. System.out.println("Truncated: " + truncatedRecord);
  31. } else {
  32. System.out.println("Original: " + record);
  33. System.out.println("Truncated: " + record); // No truncation needed
  34. }
  35. }
  36. }
  37. }

Add your comment