Advertisement
Vasilena

ТЕМА_18

Apr 25th, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.BufferedWriter;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Map;
  8. import java.io.FileReader;
  9. import java.io.BufferedReader;
  10.  
  11. public class UASD_2_190421_CW18 {
  12.     final static String outputFilePath = "C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\result.txt";
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.         HashMap<String, Integer> WordsCounter = new HashMap();
  16.  
  17.         Scanner wordsFile = new Scanner(new File("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\words.txt"));
  18.  
  19.         int counter = 0;
  20.  
  21.         while (wordsFile.hasNextLine()) {
  22.             counter = 0;
  23.             String searchWord = wordsFile.next().toLowerCase();
  24.             WordsCounter.put(searchWord, counter);
  25.  
  26.             Scanner sampleFile = new Scanner(new File("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\sample.txt"));
  27.  
  28.             while (sampleFile.hasNext()) {
  29.                 String searchedText = sampleFile.next().toLowerCase();
  30.                 if (searchedText.contains(searchWord)) {
  31.                     counter++;
  32.                 }
  33.             }//END OF FILE FOR SAMPLE.TXT//
  34.  
  35.             WordsCounter.put(searchWord, counter);
  36.             sampleFile.close();
  37.         }//END OF WHILE FOR WORDS.TXT//
  38.  
  39.         wordsFile.close();
  40.  
  41.         System.out.println("Repeats found: ");
  42.         System.out.println(WordsCounter);
  43.  
  44.         File file = new File(outputFilePath);
  45.         BufferedWriter bf = null;
  46.         ;
  47.         try {
  48.             //create new BufferedWriter for the output file
  49.             bf = new BufferedWriter(new FileWriter(file));
  50.             //iterate map entries
  51.             for (Map.Entry<String, Integer> entry : WordsCounter.entrySet()) {
  52.                 //put key and value separated by a colon
  53.                 bf.write(entry.getKey() + " = " + entry.getValue());
  54.                 //new line
  55.                 bf.newLine();
  56.             }
  57.             bf.flush();
  58.         } catch (IOException e) {
  59.             e.printStackTrace();
  60.         } finally {
  61.             try {
  62.                 //always close the writer
  63.                 bf.close();
  64.             } catch (Exception e) {
  65.             }
  66.         }
  67.  
  68.         System.out.println("This information is saved in text file!(result.txt)");
  69.  
  70.         //PRINTING THE TEXT FILE//
  71.         BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\result.txt"));
  72.         String line;
  73.         while((line = in.readLine()) != null)
  74.         {
  75.             System.out.println(line);
  76.         }
  77.         in.close();
  78.  
  79.     }//END OF MAIN//
  80. }//END OF CLASS//
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement