Skip to content

Commit

Permalink
make task
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola committed Jan 4, 2025
1 parent 160910a commit 21e2a64
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
package core.basesyntax;

import java.io.*;
import java.util.ArrayList;

public class FileWork {
public String[] readFromFile(String fileName) {
//write your code here
return null;
ArrayList<String> resultList = new ArrayList<>();
File file = new File(fileName);

try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] words = line.split("\\W+");
for (String word : words) {
if (!word.isEmpty() && word.toLowerCase().startsWith("w")) {
resultList.add(word.toLowerCase());
}
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException("Can't open the file", e);
} catch (IOException e) {
throw new RuntimeException("Error while reading the file", e);
}

return resultList.stream().sorted().toArray(String[]::new);
}
}

0 comments on commit 21e2a64

Please sign in to comment.