From 4d53beef428815443fddf9012db4ccca05e9f055 Mon Sep 17 00:00:00 2001 From: me Date: Sat, 17 Jan 2026 21:48:21 +0300 Subject: [PATCH] upd --- .../java/com/filefilter/FileFilterApp.java | 127 ++++++++++++------ file-filter/test-files/in1.txt | 8 ++ file-filter/test-files/in2.txt | 4 + 3 files changed, 98 insertions(+), 41 deletions(-) create mode 100644 file-filter/test-files/in1.txt create mode 100644 file-filter/test-files/in2.txt diff --git a/file-filter/src/main/java/com/filefilter/FileFilterApp.java b/file-filter/src/main/java/com/filefilter/FileFilterApp.java index ff6d11f..b8a51a7 100644 --- a/file-filter/src/main/java/com/filefilter/FileFilterApp.java +++ b/file-filter/src/main/java/com/filefilter/FileFilterApp.java @@ -1,10 +1,5 @@ package com.filefilter; -import picocli.CommandLine; -import picocli.CommandLine.Command; -import picocli.CommandLine.Option; -import picocli.CommandLine.Parameters; - import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -13,34 +8,47 @@ import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; +import picocli.CommandLine; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; +import picocli.CommandLine.Parameters; -@Command(name = "file-filter", - mixinStandardHelpOptions = true, - version = "1.0", - description = "Утилита фильтрации содержимого файлов по типам данных") +@Command( + name = "file-filter", + mixinStandardHelpOptions = true, + version = "1.0", + description = "utility for filtering the content of files by data types" +) public class FileFilterApp implements Runnable { @Parameters(description = "Входные файлы для обработки") private List inputFiles = new ArrayList<>(); - @Option(names = {"-o", "--output"}, - description = "Путь для выходных файлов (по умолчанию: текущая директория)") + @Option( + names = { "-o", "--output" }, + description = "Путь для выходных файлов (по умолчанию: текущая директория)" + ) private String outputPath = "."; - @Option(names = {"-p", "--prefix"}, - description = "Префикс имен выходных файлов") + @Option( + names = { "-p", "--prefix" }, + description = "Префикс имен выходных файлов" + ) private String prefix = ""; - @Option(names = {"-a", "--append"}, - description = "Режим добавления в существующие файлы") + @Option( + names = { "-a", "--append" }, + description = "Режим добавления в существующие файлы" + ) private boolean appendMode = false; - @Option(names = {"-s", "--short-stats"}, - description = "Краткая статистика") + @Option( + names = { "-s", "--short-stats" }, + description = "Краткая статистика" + ) private boolean shortStats = false; - @Option(names = {"-f", "--full-stats"}, - description = "Полная статистика") + @Option(names = { "-f", "--full-stats" }, description = "Полная статистика") private boolean fullStats = false; private Statistics statistics; @@ -82,11 +90,18 @@ public class FileFilterApp implements Runnable { } if (!Files.isReadable(filePath)) { - System.err.println("Предупреждение: файл недоступен для чтения: " + filename); + System.err.println( + "Предупреждение: файл недоступен для чтения: " + filename + ); return; } - try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { + try ( + BufferedReader reader = Files.newBufferedReader( + filePath, + StandardCharsets.UTF_8 + ) + ) { String line; int lineNumber = 0; @@ -95,7 +110,9 @@ public class FileFilterApp implements Runnable { processLine(line, filename, lineNumber); } } catch (IOException e) { - System.err.println("Ошибка при чтении файла " + filename + ": " + e.getMessage()); + System.err.println( + "Ошибка при чтении файла " + filename + ": " + e.getMessage() + ); } } @@ -113,29 +130,39 @@ public class FileFilterApp implements Runnable { writerManager.writeInteger(intValue); statistics.addInteger(intValue); break; - case FLOAT: double floatValue = Double.parseDouble(line.trim()); writerManager.writeFloat(floatValue); statistics.addFloat(floatValue); break; - case STRING: writerManager.writeString(line); statistics.addString(line); break; } } catch (IOException e) { - System.err.println("Ошибка при записи данных из файла " + filename + - ", строка " + lineNumber + ": " + e.getMessage()); + System.err.println( + "Ошибка при записи данных из файла " + + filename + + ", строка " + + lineNumber + + ": " + + e.getMessage() + ); } catch (NumberFormatException e) { // Если парсинг не удался, считаем строкой try { writerManager.writeString(line); statistics.addString(line); } catch (IOException ioException) { - System.err.println("Ошибка при записи строки из файла " + filename + - ", строка " + lineNumber + ": " + ioException.getMessage()); + System.err.println( + "Ошибка при записи строки из файла " + + filename + + ", строка " + + lineNumber + + ": " + + ioException.getMessage() + ); } } } @@ -181,20 +208,30 @@ public class FileFilterApp implements Runnable { System.out.println("=== Краткая статистика ==="); if (statistics.getIntegerCount() > 0) { - System.out.println("Целые числа: " + statistics.getIntegerCount() + " элементов"); + System.out.println( + "Целые числа: " + statistics.getIntegerCount() + " элементов" + ); } if (statistics.getFloatCount() > 0) { - System.out.println("Вещественные числа: " + statistics.getFloatCount() + " элементов"); + System.out.println( + "Вещественные числа: " + + statistics.getFloatCount() + + " элементов" + ); } if (statistics.getStringCount() > 0) { - System.out.println("Строки: " + statistics.getStringCount() + " элементов"); + System.out.println( + "Строки: " + statistics.getStringCount() + " элементов" + ); } - if (statistics.getIntegerCount() == 0 && - statistics.getFloatCount() == 0 && - statistics.getStringCount() == 0) { + if ( + statistics.getIntegerCount() == 0 && + statistics.getFloatCount() == 0 && + statistics.getStringCount() == 0 + ) { System.out.println("Данные не обработаны"); } } @@ -223,18 +260,26 @@ public class FileFilterApp implements Runnable { if (statistics.getStringCount() > 0) { System.out.println("\nСтроки:"); System.out.println(" Количество: " + statistics.getStringCount()); - System.out.println(" Длина самой короткой: " + statistics.getStringMinLength()); - System.out.println(" Длина самой длинной: " + statistics.getStringMaxLength()); + System.out.println( + " Длина самой короткой: " + statistics.getStringMinLength() + ); + System.out.println( + " Длина самой длинной: " + statistics.getStringMaxLength() + ); } - if (statistics.getIntegerCount() == 0 && - statistics.getFloatCount() == 0 && - statistics.getStringCount() == 0) { + if ( + statistics.getIntegerCount() == 0 && + statistics.getFloatCount() == 0 && + statistics.getStringCount() == 0 + ) { System.out.println("Данные не обработаны"); } } private enum DataType { - INTEGER, FLOAT, STRING + INTEGER, + FLOAT, + STRING, } -} \ No newline at end of file +} diff --git a/file-filter/test-files/in1.txt b/file-filter/test-files/in1.txt new file mode 100644 index 0000000..38f047c --- /dev/null +++ b/file-filter/test-files/in1.txt @@ -0,0 +1,8 @@ +Lorem ipsum dolor sit amet +45 +Пример +3.1415 +consectetur adipiscing +-0.001 +тестовое задание +100500 diff --git a/file-filter/test-files/in2.txt b/file-filter/test-files/in2.txt new file mode 100644 index 0000000..9376e27 --- /dev/null +++ b/file-filter/test-files/in2.txt @@ -0,0 +1,4 @@ +Нормальная форма числа с плавающей запятой +1.528535047E-25 +Long +1234567890123456789