You have an error in your code, so it is trying to process more entries than are returned from the call to
listFiles
. You should also test for the directory before creating the Scanner object, thus:
public static void main(String[] args) throws FileNotFoundException{
File file=new File("/Users/amoona/Desktop/MyDir");
File[] matches = file.listFiles();
for(int i=0;i if (matches[i].isFile()==true){
System.out.println("It Is A File");
Scanner sc=new Scanner (matches[i]);
while(sc.hasNext()){
String s=sc.next();
if ("King AbdulAziz University".equalsIgnoreCase(s)){
System.out.println(matches[i].getName());
}
}
}
}
}
You could also simplify your for loop by using a foreach, thus:
for(File ff : matches){
and use
ff
in place of
matches[i]
.