Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Scanner;
- import java.io.File;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Map;
- import java.io.FileReader;
- import java.io.BufferedReader;
- public class UASD_2_190421_CW18 {
- final static String outputFilePath = "C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\result.txt";
- public static void main(String[] args) throws IOException {
- HashMap<String, Integer> WordsCounter = new HashMap();
- Scanner wordsFile = new Scanner(new File("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\words.txt"));
- int counter = 0;
- while (wordsFile.hasNextLine()) {
- counter = 0;
- String searchWord = wordsFile.next().toLowerCase();
- WordsCounter.put(searchWord, counter);
- Scanner sampleFile = new Scanner(new File("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\sample.txt"));
- while (sampleFile.hasNext()) {
- String searchedText = sampleFile.next().toLowerCase();
- if (searchedText.contains(searchWord)) {
- counter++;
- }
- }//END OF FILE FOR SAMPLE.TXT//
- WordsCounter.put(searchWord, counter);
- sampleFile.close();
- }//END OF WHILE FOR WORDS.TXT//
- wordsFile.close();
- System.out.println("Repeats found: ");
- System.out.println(WordsCounter);
- File file = new File(outputFilePath);
- BufferedWriter bf = null;
- ;
- try {
- //create new BufferedWriter for the output file
- bf = new BufferedWriter(new FileWriter(file));
- //iterate map entries
- for (Map.Entry<String, Integer> entry : WordsCounter.entrySet()) {
- //put key and value separated by a colon
- bf.write(entry.getKey() + " = " + entry.getValue());
- //new line
- bf.newLine();
- }
- bf.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- //always close the writer
- bf.close();
- } catch (Exception e) {
- }
- }
- System.out.println("This information is saved in text file!(result.txt)");
- //PRINTING THE TEXT FILE//
- BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\vasil\\IdeaProjects\\UASD2\\UASD_18\\result.txt"));
- String line;
- while((line = in.readLine()) != null)
- {
- System.out.println(line);
- }
- in.close();
- }//END OF MAIN//
- }//END OF CLASS//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement