Skip to content

Commit

Permalink
reading from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazar1106 committed Sep 25, 2024
1 parent 160910a commit 29cf5a4
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,43 @@
package core.basesyntax;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileWork {
private static final String SPECIFIED_CHARACTER = "w";
private static final String WORD_SEPARATOR = " ";

public String[] readFromFile(String fileName) {
//write your code here
return null;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
String read = bufferedReader.readLine();
List<String> stringList = new ArrayList<>();
while (read != null) {
read = read.toLowerCase();
String[] words = read.split(WORD_SEPARATOR);
for (String word : words) {
if (word.startsWith(SPECIFIED_CHARACTER)) {
stringList.add(word.replaceAll("\\p{Punct}", ""));
}
}
Collections.sort(stringList);
read = bufferedReader.readLine();
}
String[] stringArray = new String[stringList.size()];

for (int i = 0; i < stringArray.length; i++) {
stringArray[i] = stringList.get(i);
}
if (stringList.isEmpty()) {
return new String[]{};
} else {
return stringArray;
}
} catch (IOException e) {
throw new RuntimeException("File not found exception");
}
}
}

0 comments on commit 29cf5a4

Please sign in to comment.