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
1 parent
cecd533
commit a1375f4
Showing
2 changed files
with
33 additions
and
3 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
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,39 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.util.Arrays; | ||
|
||
public class FileWork { | ||
public String[] readFromFile(String fileName) { | ||
//write your code here | ||
return null; | ||
File file = new File(fileName); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
try { | ||
BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); | ||
String value = bufferedReader.readLine(); | ||
|
||
while (value != null) { | ||
String[] strings = value.split(" "); | ||
for (String str : strings) { | ||
String st = str.toLowerCase().replaceAll("[!?.]",""); | ||
if (st.startsWith("w")) { | ||
stringBuilder.append(st).append(" "); | ||
} | ||
} | ||
|
||
String ff = stringBuilder.toString(); | ||
value = bufferedReader.readLine(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("Can't read file",e); | ||
} | ||
if (!stringBuilder.isEmpty()) { | ||
String[] str = stringBuilder.toString().split(" "); | ||
Arrays.sort(str); | ||
return str; | ||
} else { | ||
return new String[0]; | ||
} | ||
} | ||
} |