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
Showing
1 changed file
with
37 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,43 @@ | ||
package core.basesyntax; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
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))) { | ||
int value = bufferedReader.read(); | ||
while (value != -1) { | ||
stringBuilder.append((char)value); | ||
value = bufferedReader.read(); | ||
|
||
} | ||
|
||
} catch (FileNotFoundException e) { | ||
throw new RuntimeException("can't read file",e); | ||
} catch (IOException e) { | ||
throw new RuntimeException("cant't read ",e); | ||
} | ||
String[] values = stringBuilder.toString().split("\\W+"); | ||
StringBuilder update = new StringBuilder(); | ||
for (String value:values) { | ||
if (value.startsWith("W") || value.startsWith("w")) { | ||
update.append(value.toLowerCase()).append(" "); | ||
} | ||
} | ||
if (!update.isEmpty()) { | ||
String[] result = update.toString().split(" "); | ||
Arrays.sort(result); | ||
return result; | ||
} | ||
|
||
return new String[0]; | ||
} | ||
} |