From a1375f47e7d5f55f3bba60badb257f0e72cbcef8 Mon Sep 17 00:00:00 2001 From: Bohdan Chupika Date: Mon, 13 Nov 2023 13:05:07 +0200 Subject: [PATCH] create FileWork --- pom.xml | 1 - src/main/java/core/basesyntax/FileWork.java | 35 +++++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 3bfcdbea..cf842bae 100644 --- a/pom.xml +++ b/pom.xml @@ -41,7 +41,6 @@ ${maven.checkstyle.plugin.configLocation} - UTF-8 true true false diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..49150f8d 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,39 @@ package core.basesyntax; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.util.Arrays; + public class FileWork { public String[] readFromFile(String fileName) { - //write your code here - return null; + File file = new File(fileName); + StringBuilder stringBuilder = new StringBuilder(); + try { + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + String value = bufferedReader.readLine(); + + while (value != null) { + String[] strings = value.split(" "); + for (String str : strings) { + String st = str.toLowerCase().replaceAll("[!?.]",""); + if (st.startsWith("w")) { + stringBuilder.append(st).append(" "); + } + } + + String ff = stringBuilder.toString(); + value = bufferedReader.readLine(); + } + } catch (Exception e) { + throw new RuntimeException("Can't read file",e); + } + if (!stringBuilder.isEmpty()) { + String[] str = stringBuilder.toString().split(" "); + Arrays.sort(str); + return str; + } else { + return new String[0]; + } } }