I need to input multiple files from directory

but how to do it for any file name? If I do not know the file name
So I can pass one by one file to thread?

 int 1 = 0;
    while(true)
    {
        String name = "Lab3File"+i+".dat";
        File file = new File(name);
        if(file.exists())
        {
            try
            {   
                Thread thread = new Thread(new InnerClass(file));
                thread.start();
                thread.join();
            }
            catch(InterruptedException ie)
            {
            }
        }
        else
        {
            break;
        }
        i++;
    }

Edited 4 Years Ago by mehnihma: wring

something like this should work...

File rootDir = new File("/Path/to/dir/");
File[] filesAndDirectories = rootDir.listFiles();

the filesAndDirectories array contains everything in rootDir. You can search for filename given a regexp or something, go down to another directory etc...

Edited 4 Years Ago by SasseMan: bad tag

I am trying something like this

import java.io.*;
import java.util.*;

public class Lab3part1
{

  public void read(File file)
  {
    // if directory
     if (file.isDirectory())
     {


        FilenameFilter select = new FileListFilter("", "dat");

        File[] contents = file.listFiles();

        for (File thisFile: contents)
        {
               read(thisFile);


                System.out.println("This is a directory\t " + thisFile.getName());

        }

     }
     // if data file
     else if(file.isFile())
     {

         System.out.println("This is a file\t" + file.getName());

     }
     else
     {

     }


  }

  class FileListFilter implements FilenameFilter {
     private String name; 

     private String extension; 

     public FileListFilter(String name, String extension) {
        this.name = name;
        this.extension = extension;
     }

     public boolean accept(File directory, String filename) {
        boolean fileOK = true;

        if (name != null) {
           fileOK &= filename.startsWith(name);
        }

        if (extension != null) {
           fileOK &= filename.endsWith('.' + extension);
        }
        return fileOK;
     }
  }


  public Lab3part1()
  {

     File rootDir = new File("/Lab3Data/");
     File[] filesAndDirectories = rootDir.listFiles();

     System.out.println(rootDir.listFiles());




  }



  public static void main(String[] arguments) {

    Lab3part1 lab3part1 = new Lab3part1();

      lab3part1.read(new File("txtfiles"));




  }

}

What happens when you execute the code?
Do you get the results you want?
If not, please explain.

This recursion works, but I am intereset how to do it simple? That is what I got so far

This article has been dead for over six months. Start a new discussion instead.