I have a String array with the list of files (files and folders) of a path:
File directory = new File(path);
String[] fileNames = directory.list();
When I try to read the file content:
for (int i = 0; i < fileNames.length; i++) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(path + "\\" + fileNames[i]), "UTF-8"));
If the file is a folder I get a FileNotFoundException.
How can I know before if a file name belongs to a folder. Perhaps by the size of the file?
You can use
File#isDirectorymethod to test whether afileis adirectory.Or, you can use
File#listFilesmethod instead. It only returns thefilesinside the directory. That way, you won’t have to check whether yourfileis adirectoryor not.