2 Replies Latest reply: Mar 10, 2007 10:56 AM by 807606 RSS

    read multiple files from a directory

    807606
      Hello,

      I'm wondering how I can read load/read all the files from a certain directory without knowing their names.
      Can I somehow load them one by one and process them?


      Regards,
      Pesho
        • 1. Re: read multiple files from a directory
          800649
          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;
          }
          }

          // It is also possible to filter the list of returned files.
          // This example does not return any files that start with `.'.
          FilenameFilter filter = new FilenameFilter() {
          public boolean accept(File dir, String name) {
          return !name.startsWith(".");
          }
          };
          children = dir.list(filter);


          // The list of files can also be retrieved as File objects
          File[] files = dir.listFiles();

          // This filter only returns directories
          FileFilter fileFilter = new FileFilter() {
          public boolean accept(File file) {
          return file.isDirectory();
          }
          };
          files = dir.listFiles(fileFilter);
          • 2. Re: read multiple files from a directory
            807606
            Thanks !