Java:在指定文件夹中查找.txt文件

有没有内置的Java代码将parsing给定的文件夹,并search.txt文件?

您可以使用java.io.File类提供的listFiles()方法。

 import java.io.File; import java.io.FilenameFilter; public class Filter { public File[] finder( String dirName){ File dir = new File(dirName); return dir.listFiles(new FilenameFilter() { public boolean accept(File dir, String filename) { return filename.endsWith(".txt"); } } ); } } 

尝试:

 List<String> textFiles(String directory) { List<String> textFiles = new ArrayList<String>(); File dir = new File(directory); for (File file : dir.listFiles()) { if (file.getName().endsWith((".txt"))) { textFiles.add(file.getName()); } } return textFiles; } 

你想在这种情况下做一个不区分大小写的search:

  if (file.getName().toLowerCase().endsWith((".txt"))) { 

如果你想recursionsearch通过文本文件的目录树,你应该能够适应上述作为一个recursion函数或迭代函数使用堆栈。

 import org.apache.commons.io.filefilter.WildcardFileFilter; ......... ......... File dir = new File(fileDir); FileFilter fileFilter = new WildcardFileFilter("*.txt"); File[] files = dir.listFiles(fileFilter); 

上面的代码对我很好

这真的很有用,我稍微改变了一下:

 filename=directory.list(new FilenameFilter() { public boolean accept(File dir, String filename) { return filename.startsWith(ipro); } }); 

我做了我的解决scheme,基于我在谷歌发现这里的post。 而且我认为即使是旧线程也不会伤害我的。

这个代码的唯一优点是它可以遍历子目录。

 import java.io.File; import java.io.FileFilter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; 
 List <File> exploreThis(String dirPath) { File topDir = new File(dirPath); List<File> directories = new ArrayList<>(); directories.add(topDir); List<File> textFiles = new ArrayList<>(); List<String> filterWildcards = new ArrayList<>(); filterWildcards.add("*.txt"); filterWildcards.add("*.doc"); FileFilter typeFilter = new WildcardFileFilter(filterWildcards); while (directories.isEmpty() == false) { List<File> subDirectories = new ArrayList(); for(File f : directories) { subDirectories.addAll(Arrays.asList(f.listFiles((FileFilter)DirectoryFileFilter.INSTANCE))); textFiles.addAll(Arrays.asList(f.listFiles(typeFilter))); } directories.clear(); directories.addAll(subDirectories); } return textFiles; } 
 import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.FileVisitResult; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; public class FileFinder extends SimpleFileVisitor<Path> { private PathMatcher matcher; public ArrayList<Path> foundPaths = new ArrayList<>(); public FileFinder(String pattern) { matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern); } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Path name = file.getFileName(); if (matcher.matches(name)) { foundPaths.add(file); } return FileVisitResult.CONTINUE; } } import java.io.IOException; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { Path fileDir = Paths.get("files"); FileFinder finder = new FileFinder("*.txt"); Files.walkFileTree(fileDir, finder); ArrayList<Path> foundFiles = finder.foundPaths; if (foundFiles.size() > 0) { for (Path path : foundFiles) { System.out.println(path.toRealPath(LinkOption.NOFOLLOW_LINKS)); } } else { System.out.println("No files were founds!"); } } } 

这是我的平台特定的代码(Unix)

 public static List<File> findFiles(String dir, String... names) { LinkedList<String> command = new LinkedList<String>(); command.add("/usr/bin/find"); command.add(dir); List<File> result = new LinkedList<File>(); if (names.length > 1) { List<String> newNames = new LinkedList<String>(Arrays.asList(names)); String first = newNames.remove(0); command.add("-name"); command.add(first); for (String newName : newNames) { command.add("-or"); command.add("-name"); command.add(newName); } } else if (names.length > 0) { command.add("-name"); command.add(names[0]); } try { ProcessBuilder pb = new ProcessBuilder(command); Process p = pb.start(); p.waitFor(); InputStream is = p.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { // System.err.println(line); result.add(new File(line)); } p.destroy(); } catch (Exception e) { e.printStackTrace(); } return result; } 

import org.apache.commons.io.FileUtils;

List htmFileList = new ArrayList();

(File):(List)FileUtils.listFiles(new File(srcDir),new String [] {“txt”,“TXT”},true)){htmFileList.add(file); }

这是我最新的代码来添加目录中的所有文本文件