Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.*;
- import java.util.stream.Collectors;
- public class streams1_Exercises_6Word_Count {
- public static void main(String[] args) {
- String input = "C:\\Users\\kk\\Desktop\\SoftUni\\Java Advanced\\Lections\\" +
- "04. Java-Exercises_Advanced-Streams-Files-and-Directories-Resources (1)\\" +
- "04. Java-Advanced-Files-and-Streams-Exercises-Resources\\";
- String pathIn = input + "words.txt";
- String pathInTwo = input + "text.txt";
- String pathOut = input + "wordsResults.txt";
- try {
- try (BufferedReader bf = Files.newBufferedReader(Paths.get(pathIn))) {
- try (BufferedReader bfTwo = Files.newBufferedReader(Paths.get(pathInTwo))) {
- try (PrintWriter write = new PrintWriter(pathOut)) {
- List<String> text = Arrays.stream(bf.readLine().split("\\s+"))
- .collect(Collectors.toList());
- Map<String, Integer> word = new HashMap<>();
- for (String value : text) {
- word.putIfAbsent(value, 0);
- }
- String readBfTwo = bfTwo.readLine();
- List<String> textTwo = new ArrayList<>();
- while (readBfTwo != null) {
- String[] textArr = readBfTwo.split("\\s+");
- for (String s : textArr) {
- if (word.containsKey(s)) {
- word.put(s, word.get(s) + 1);
- }
- }
- readBfTwo = bfTwo.readLine();
- }
- word.entrySet().stream().sorted((f, s) -> s.getValue()
- .compareTo(f.getValue())).forEach(e ->
- write.printf("%s - %d%n", e.getKey(), e.getValue()));
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment