Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make task #1413

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
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);
}
}
Loading