package task3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class task3 {

    // Проверка: начинается ли слово с русской согласной
    private static boolean startsWithConsonant(String word) {
        if (word.isEmpty()) return false;
        char c = Character.toLowerCase(word.charAt(0));
        return "бвгджзйклмнпрстфхцчшщ".indexOf(c) >= 0;
    }

    public static void main(String[] args) {

        String inputFile = "Files/input.txt";
        String outputFile = "Files/output.txt";

        BufferedReader reader = null;
        BufferedWriter writer = null;

        try {
            reader = new BufferedReader(new FileReader(inputFile));
            writer = new BufferedWriter(new FileWriter(outputFile));

            String line;
            int lineIndex = 1;

            while ((line = reader.readLine()) != null) {

                String[] parts = line.split("[\\s\\p{Punct}]+");
                int found = 0;

                writer.write("Строка №" + lineIndex);
                writer.newLine();

                for (String p : parts) {
                    if (startsWithConsonant(p)) {
                        writer.write(p);
                        writer.newLine();
                        found++;
                    }
                }

                writer.write("Всего слов: " + found);
                writer.newLine();
                writer.newLine();

                lineIndex++;
            }

        } catch (IOException e) {
            System.out.println("Ошибка: " + e);
        } finally {
            try {
                if (reader != null) reader.close();
                if (writer != null) {
                    writer.flush();
                    writer.close();
                }
            } catch (IOException e) {
                // молча
            }
        }
    }
}