Number of Occurrences of a word in a text file or group of text files The question given to me was as follows : 
WAP to take the input from the user a path and a word. If the path is a text file, search the word in the file and print the number of occurrences. If the path is a folder, read the files in the folder and print the number of occurrences in all the text files of the word.

Is what I did an effecient way of going about it? Please ignore indentations and lack of comments. 

This was my code : 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class TestOccurances {

public static void main(String[] args) {
Scanner sc1 = new Scanner(System.in);
String path, word;
System.out.println("Enter a path");
path = sc1.nextLine();
System.out.println("Enter the word");
word = sc1.nextLine();
sc1.close();
File f = new File(path);
BufferedReader br = null;
List&lt;String&gt; list = new ArrayList&lt;String&gt;();
try {
//Checking to see if its a .txt file
if(f.exists() &amp;&amp; f.isFile() &amp;&amp; f.getName().endsWith(".txt")){
    br  = new BufferedReader(new FileReader(path));
    String line;
    while((line=br.readLine())!=null) {
        String[] arr = line.split(" ");
        for(String str : arr)
        list.add(str);
        }
    System.out.println("Single text file, Frequency = "+Collections.frequency(list, word));
    }
//Checking to see if its a folder
if(f.exists() &amp;&amp; f.isDirectory()) {
    File[] allfiles = f.listFiles();
    List&lt;File&gt;  files= new ArrayList&lt;File&gt;();
    for(File f1 : allfiles) {
        if(f1.getName().endsWith(".txt"))
            files.add(f1);
    }
    //Loop through each text file and do the same as above
    for(File file : files) {
        List&lt;String&gt; occurances = new ArrayList&lt;String&gt;();
        br = new BufferedReader(new FileReader(file.getPath()));
        String line;
        while((line=br.readLine())!=null) {
            String[] arr = line.split(" ");
            for(String str : arr)
            occurances.add(str);
            }
        System.out.println("File path :"+file.getName()+" , Frequency = "+Collections.frequency(occurances, word));
        }
    }
}catch(IOException e) {
    e.printStackTrace();
    }
finally {
    if(br!=null) {
        try {
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    }
}


}
While the functional approach may not always be the best solution, I find using Streams on files very convenient. 

If you need to extend the second requirement on directories even further to include all .txt files in subdirectories, you can easily recurse through and flatmap all of the subdirectories until you are left with just a single stream of .txt files.

Code:

public static void main(String[] args) throws IOException {

    Scanner inputReader = new Scanner(System.in);

    System.out.println("Enter a path");
    String inputPath = inputReader.nextLine();
    System.out.println("Enter the word");
    String targetWord = inputReader.nextLine();

    File file = new File(inputPath);

    long numOccurances = 0;

    if(file.isFile() &amp;&amp; file.getName().endsWith(".txt")){

        numOccurances = toLineStreamFromFile(file)
                            .flatMap(str -&gt; Arrays.stream(str.split("\\s")))
                            .filter(str -&gt; str.equals(targetWord))
                            .count();

    }else if(file.isDirectory()){

        numOccurances = Arrays.stream(file.listFiles(pathname -&gt; pathname.toString().endsWith(".txt")))
                              .flatMap(Main::toLineStreamFromFile)
                              .flatMap(str -&gt; Arrays.stream(str.split("\\s")))
                              .filter(str -&gt; str.equals(targetWord))
                              .count();
    }

    System.out.println(numOccurances);
}

public static Stream&lt;String&gt; toLineStreamFromFile(File file){
    try {
        return Files.lines(file.toPath());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Stream.empty();
}