From b22cb49bcbe620dbfb5ede03a701f56de30549db Mon Sep 17 00:00:00 2001 From: Tony Hut Date: Mon, 16 Oct 2023 11:29:50 +0200 Subject: [PATCH] commit --- src/main/java/core/basesyntax/FileWork.java | 38 +++++++++++++++++++-- src/main/java/core/basesyntax/Main.java | 20 +++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/main/java/core/basesyntax/Main.java diff --git a/src/main/java/core/basesyntax/FileWork.java b/src/main/java/core/basesyntax/FileWork.java index ba2d8396..38b884c9 100644 --- a/src/main/java/core/basesyntax/FileWork.java +++ b/src/main/java/core/basesyntax/FileWork.java @@ -1,8 +1,42 @@ package core.basesyntax; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.Arrays; + public class FileWork { public String[] readFromFile(String fileName) { - //write your code here - return null; + StringBuilder builder = new StringBuilder(); + StringBuilder builder1 = new StringBuilder(); + + try { + BufferedReader reader = new BufferedReader(new FileReader(fileName)); + int value = reader.read(); + if (value == -1) { + return new String[]{}; + } + while (value != -1) { + builder.append((char) value); + value = reader.read(); + } + System.out.println(builder.toString()); + } catch (IOException e) { + throw new RuntimeException(e); + } + String hello = builder.toString(); + String[] first = hello.split(" "); + for (int i = 0; i < first.length; i++) { + if (first[i].startsWith("W") || first[i].startsWith("w")) { + builder1.append(first[i].toLowerCase()); + builder1.append(" "); + } + } + if (builder1.isEmpty()) { + return new String[]{}; + } + String[] finish = builder1.toString().split(("\\W+")); + Arrays.sort(finish); + return finish; } } diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 00000000..38e44c9d --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,20 @@ +package core.basesyntax; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) { + File file = new File("example.txt"); + try { + file.createNewFile(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + FileWork fileWork = new FileWork(); + System.out.println(Arrays.toString(fileWork.readFromFile("example.txt"))); + + } +}