I need to get names of a lot of folders and files in the folders. I tried using FileUtils
, but it's too slow compared to java's native Files.walk()
, and I can't use it because of that reason.
But I can't even use the java's Files.walk()
because when it tries to read a hidden file, it throws Exception
and stops (the hidden file cannot be avoided, someone said it's a design mistake - Files.walk skip directories ). Is there any different way? I don't know what to do now.
Kindly do the following in your code.-
import org.apache.commons.io.filefilter.HiddenFileFilter;
I will give you an example-
package test;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import org.apache.commons.io.filefilter.HiddenFileFilter;
public class ListHiddenAndVisibleFilesTest {
public static void main(String[] args) throws IOException {
File directory = new File(".");
System.out.println("Hidden Files:");
File[] hiddenFiles = directory.listFiles((FileFilter) HiddenFileFilter.HIDDEN);
for (File hiddenFile : hiddenFiles) {
System.out.println("hidden file: " + hiddenFile.getCanonicalPath());
}
System.out.println("\nVisible Files:");
File[] visibleFiles = directory.listFiles((FileFilter) HiddenFileFilter.VISIBLE);
for (File visibleFile : visibleFiles) {
System.out.println("visible file: " + visibleFile.getCanonicalPath());
}
}
}
This gives result like
Hidden Files:
hidden file: C:\kum\workspace\test\hidden1.txt
hidden file: C:\kum\workspace\test\hidden2.txt
Visible Files:
visible file: C:\kum\workspace\test\.classpath
visible file: C:\kum\workspace\test\.project
visible file: C:\kum\workspace\test\visibleFile.txt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.