Advertisement
cska1312

FIles

Dec 27th, 2022
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Properties;
  6. public class Demo {
  7.     public static void main(String[] args) throws IOException {
  8.         Properties properties = System.getProperties();
  9.         Object o = properties.get("user.home");
  10.         File file;
  11.         file = new File(pathname: "text1.txt")
  12.         String theWords = "can";
  13.         BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
  14.         String line;
  15.         while((line = bufferedReader.readLine()) != null){
  16.             if(line.contains(theWords)){
  17.                 System.out.println("This line contains " + theWords);
  18.                 String[] s = line.split( regex: "[\\s.!]+");
  19.                 int counter = 0;
  20.                 for(int i = 0; i < s.length; i++){
  21.                     if(s[i].equals(theWords)){
  22.                         counter++;
  23.                     }
  24.                 }
  25.                 if(counter != 0){
  26.                     System.out.println("The word " + theWords + " is found " + counter + " times.");
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement