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

commit #1028

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

commit #1028

Show file tree
Hide file tree
Changes from all 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
38 changes: 36 additions & 2 deletions src/main/java/core/basesyntax/FileWork.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
package core.basesyntax;

import java.io.BufferedReader;
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;
StringBuilder builder = new StringBuilder();
StringBuilder builder1 = new StringBuilder();

try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
int value = reader.read();
if (value == -1) {
return new String[]{};
}
while (value != -1) {
builder.append((char) value);
value = reader.read();
}
System.out.println(builder.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
String hello = builder.toString();
String[] first = hello.split(" ");
for (int i = 0; i < first.length; i++) {
if (first[i].startsWith("W") || first[i].startsWith("w")) {
builder1.append(first[i].toLowerCase());
builder1.append(" ");
}
}
if (builder1.isEmpty()) {
return new String[]{};
}
String[] finish = builder1.toString().split(("\\W+"));
Arrays.sort(finish);
return finish;
}
}
20 changes: 20 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package core.basesyntax;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

public class Main {
public static void main(String[] args) {
File file = new File("example.txt");
try {
file.createNewFile();
} catch (IOException e) {
throw new RuntimeException(e);
}

FileWork fileWork = new FileWork();
System.out.println(Arrays.toString(fileWork.readFromFile("example.txt")));

}
}
Loading