How to use java to get all text files from a folder?
•
Java
I need to read all ". TXT" files from the folder (the user needs to select this folder)
Please tell me how to do it?
Solution
You can use the filenamefilter class, which is a very simple usage
File f = new File("c:\\mydirectory"); FilenameFilter textFilter = new FilenameFilter() { public boolean accept(File dir,String name) { return name.toLowerCase().endsWith(".txt"); } }; File[] files = f.listFiles(textFilter); for (File file : files) { if (file.isDirectory()) { System.out.print("directory:"); } else { System.out.print(" file:"); } System.out.println(file.getCanonicalPath()); } }
Just create a filenamefilter instance, and then create the override acceptance method as needed
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码