My problem is that I want to iterate all the text files in a directory using Java but whenever I start for loop over files, it always results in half processing, like for example:- I have 80 files and I want to read content of each file then I start my loop, it executes only last half part of file.
Note: I have tried FileInputStream, Scanner class, BufferedReader as well. In FileInputStream, it gives EOFException and half-iteration results for other two methods.
Here is my code -
package InputFiles;
import java.io.*;
import java.util.Scanner;
public class FileInput {
public static void main(String[] args) {
File folder=new File("C:\\Users\\erpra\\Downloads\\SomePath");
File[] listOfFiles=folder.listFiles();
BufferedReader br=null;
try {
for(File tempFile:listOfFiles){
System.out.println(tempFile.getName());
br=new BufferedReader(new FileReader(tempFile));
String str;
while ((str=br.readLine())!=null){
System.out.println(str);
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}