Member Avatar for mehnihma

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++;
    }

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...

Member Avatar for mehnihma

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.

Member Avatar for mehnihma

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.