集册 Java实例教程 列出目录中的文件或子目录

列出目录中的文件或子目录

欢马劈雪     最近更新时间:2020-01-02 10:19:05

521
列出目录中的文件或子目录

import java.io.File;

import java.io.FileFilter;

import java.io.FilenameFilter;

/**来自 
 n  o  w  j  a  v  a . c o m**/

public class Main {

  public static void main(String[] argv) {

    File dir = new File("directoryName");


    String[] children = dir.list();

    if (children == null) {

      // Either dir does not exist or is not a directory

    } else {

      for (int i = 0; i < children.length; i++) {

        // Get filename of file or directory

        String filename = children[i];

      }

    }

    /*
    时 代 J a v a - nowjava.com 提供
    */

    FilenameFilter filter = new FilenameFilter() {

      public boolean accept(File dir, String name) {

        return !name.startsWith(".");

      }

    };

    children = dir.list(filter);


    File[] files = dir.listFiles();


    // This filter only returns directories

    FileFilter fileFilter =
展开阅读全文