generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mykola
committed
Jan 4, 2025
1 parent
160910a
commit 21e2a64
Showing
1 changed file
with
23 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |