Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void reader(String fileName) throws IOException {
- try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
- String line;
- List<String> lineHolder = new ArrayList<>();
- StringTokenizer divider;
- String formatter = "%2s|%-30s|%-30s|%10s|%n";
- String separator = "----------";
- System.out.format("ID|%-30s|%-30s|birth date|%n", "name", "email");
- System.out.print("--+" + separator + separator + separator
- + "+" + separator + separator + separator + "+"
- + separator + "+n");
- while ((line = br.readLine()) != null){
- if (line.startsWith("#", 0))
- continue;
- if (!line.contains(";")) {
- throw new IOException("too less data or not proper delimiter");
- }
- divider = new StringTokenizer(line, ";");
- lineHolder = arrayFiller(divider);
- dataChecker(lineHolder, line);
- System.out.format(formatter, lineHolder.get(0), lineHolder.get(1)
- , lineHolder.get(2), lineHolder.get(3));
- }
- } catch (FileNotFoundException ex) {
- System.err.println("The file not found.");
- } catch (IOException ex){
- System.err.println(ex.getMessage());
- }
- System.out.print("n");
- }
- public static ArrayList<String> arrayFiller(StringTokenizer divider) {
- ArrayList<String> lineHolder = new ArrayList<>();
- while (divider.hasMoreTokens()) {
- lineHolder.add(divider.nextToken());
- }
- return lineHolder;
- }
- public static void dataChecker(List<String> lineHolder, String line) throws IOException {
- if (lineHolder.size() < 4) {
- throw new IOException("too less data or not proper delimiter");
- } else if (lineHolder.size() > 4) {
- throw new IOException("too much data");
- } else if (lineHolder.get(0).length() > 2
- || !Pattern.matches("[0-9]+", lineHolder.get(0))) {
- throw new IOException("Error during reading the file: "
- + "not proper ID field format");
- } else if (lineHolder.get(1).length() > 30
- || !Pattern.matches("[a-zA-ZíÍöÖüÜóÓőŐúÚűŰáÁéÉ. ]+", lineHolder.get(1))) {
- throw new IOException("Error during reading the file: "
- + "not proper Name field format");
- } else if (lineHolder.get(2).length() > 30
- || !Pattern.matches("[a-zA-Z0-9@. ]+", lineHolder.get(2))) {
- throw new IOException("Error during reading the file: "
- + "not proper Email field format");
- } else if (lineHolder.get(3).length() > 10 || dateFormatter(lineHolder.get(3))) {
- throw new IOException("Error during reading the file: "
- + "not proper Birth date field format");
- }
- }
- public static boolean dateFormatter(String datum) {
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
- try {
- LocalDate changedDate = LocalDate.parse(datum, dtf);
- return false;
- } catch (DateTimeParseException ex) {
- return true;
- }
- }
- #ID;name;email;birth date
- 1,Jakob George,gipszjakab@gmail.com,1981-11-23
- 2;Susan Smith;usa@gmail.com;1982-12-01
- 3;Poul Drake;gameover@gmail.com;1990-01-02
- 4;Isaac Wheather;ruck%sack@freemail.hu;1988-01-22
- 5;George T. Benson;bigman@hotmail.com;1977-08-12
Add Comment
Please, Sign In to add comment