diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..51c77fa6 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,32 @@ package core.basesyntax; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + public class FileWork { + public String[] readFromFile(String fileName) { - //write your code here - return null; + + List result; + try { + result = Files.readAllLines(Paths.get(fileName)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + List sortedResult = new ArrayList<>(); + for (String s : result) { + sortedResult.addAll(Arrays.stream(s.split("\\W+")) + .map(String::toLowerCase) + .filter(word -> word.startsWith("w")) + .toList()); + } + Collections.sort(sortedResult); + return sortedResult.toArray(new String[0]); } }