In the program below I'm currently reading files in a directory. However I'd like to only read .txt files. How can I read only the .text files located in the directory.
import java.io.*;
public class Data {
public static void main(String[] args) throws IOException {
String target_dir = "C:\\Files";
String textfile;
File dir = new File(target_dir);
File[] files = dir.listFiles();
for (File textfiles : files) {
if (textfiles.isFile()) {
BufferedReader inputStream = null;
try {
inputStream = new BufferedReader(new FileReader(textfiles));
String line;
while ((line = inputStream.readLine()) != null) {
System.out.println(line);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}
}
}