Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Properties;
- public class Demo {
- public static void main(String[] args) throws IOException {
- Properties properties = System.getProperties();
- Object o = properties.get("user.home");
- File file;
- file = new File(pathname: "text1.txt")
- String theWords = "can";
- BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
- String line;
- while((line = bufferedReader.readLine()) != null){
- if(line.contains(theWords)){
- System.out.println("This line contains " + theWords);
- String[] s = line.split( regex: "[\\s.!]+");
- int counter = 0;
- for(int i = 0; i < s.length; i++){
- if(s[i].equals(theWords)){
- counter++;
- }
- }
- if(counter != 0){
- System.out.println("The word " + theWords + " is found " + counter + " times.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement