Untitled
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Exception { public static void main(String[] args) { Container container; Checker checker = new Checker(); try { container = Container.fromFile("file.txt"); checker.checkDelimiter(container, ","); } catch (FileNotFoundException | DelimiterException e) { System.err.println(e.getMessage()); } } public static class Container { private final List<String> lines = new ArrayList<>(); public static Container fromFile(String filePath) { try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { Container container = new Container(); String line; while ((line = br.readLine()) != null) { container.lines.add(line); } return container; } catch (IOException e) { throw new FileNotFoundException("Error reading file: " + e.getMessage(), e); } } public int getLineCount() { return lines.size(); } public String getLine(int index) { return lines.get(index); } } public static class Checker extends RuntimeException { private void checkDelimiter(Container container, String delimiter) { for (int i = 0; i < container.getLineCount(); i++) { var line = container.getLine(i); if (!line.endsWith(delimiter)) { throw new DelimiterException("Line does not end with delimiter: " + line); } } } } public static class FileNotFoundException extends RuntimeException { public FileNotFoundException(String message, Throwable cause) { super(message, cause); } } public static class DelimiterException extends RuntimeException { public DelimiterException(String message) { super(message); } } }
Leave a Comment