Java Io FilenameFilter

Hi Team,

I have writtenj the below code for displaying the *.txt and *.class files in the directory mentioned by the user at the run time.

My question is
What values are passed to the accept method when I invoke the method list as shown below in the code
String [] names = fileObj.list(ext); I know it invokes the accept method , but i want to know what values are passed to the parameters dirname and extn when it is invoked.

I have created the fileobject to refer to the folder D:\javaPrg,. In this folder I have the files with txt and class extensions.

I run the program as
D:\JavaPrg>java FileExtnDemo D:\JavaPrg .txt

Output I get is
D:\JavaPrg>java FileExtnDemo D:\JavaPrg .txt
The names are:
File1.txt
FileDemo.class
FileExtnDemo.class
MyfilenameFilter.class



import java.io.*;

class MyfilenameFilter implements FilenameFilter
{
@Override
public boolean accept (File dirname , String extn)
{
      if (extn.endsWith(".class"))
      {
            return true;
      }
      else if(extn.endsWith(".txt"))
      {
      return true;
      }
      else
      {
      return false;
      }
}

}

public class FileExtnDemo
{

public static void main(String [] args)
{
      MyfilenameFilter ext = new MyfilenameFilter();
      
      File fileObj = new File(args[0]);
      
      String [] fileNames = fileObj.list(ext);
      System.out.println("The names are:");
      for(String names:fileNames)
      {
      System.out.println(names);
      }
      
      

}

}
sam_2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

awking00Information Technology SpecialistCommented:
What's the code for the class fileObj?
0
Jan LouwerensSoftware EngineerCommented:
I'm not entirely clear on exactly what you're asking, but if I'm interpreting the question correctly, the accept() method would be called once for every file within the D:\JavaPrg folder, where "dirname" would be a File object that represents the D:\JavaPrg folder, and "extn" would be the extension of the file.

The ".txt" command line parameter that you're passing into the FileExtnDemo program is not used (since you're only looking at args[0], and not args[1])
0
sam_2012Author Commented:
hi

my question is does the method public boolean accept (File dirname , String extn), String extn store the filename with extension in the given file object  path or only extension of the file?
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

sam_2012Author Commented:
Hi The code is there in the question itself.
0
Jan LouwerensSoftware EngineerCommented:
The extn parameter contains only the extension of the file, not the entire filename.
0
David Johnson, CD, MVPOwnerCommented:
It would appear that by calling the program.exe c:\ txt that you want only txt files from c:\ but you don't ever reference argc[1] and your code is hard coded to return .class and .txt filenames
public boolean accept (File dirname , String extn)
{
      if ((extn.endsWith(".class")) || (extn.endsWith(".txt")))   { return true; }
      else { return false;  }
}

Open in new window

public class FileExtnDemo
{

    public static void main(String [] args)
    {
    MyfilenameFilter ext = new MyfilenameFilter();
      
    File fileObj = new File(args[0]);
// you need to add code here to deal with more argv's
if (argc()  < 3) {
      [] fileNames = fileObj.list(ext);
}
else {
// here you need to parse argv and get the list of extensions and then for each extension you need to search and then output the results
}
    System.out.println("The names are:");
    for(String names:fileNames) {
        System.out.println(names);
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
If you are using Java 8 or 9, then you could use something like the following.
import java.nio.file.*;
import java.util.stream.Stream;
import java.io.IOException;
public class FileDemo {
    public static void main(String arg[]){
		final Path path = FileSystems.getDefault().getPath("D:\JavaPrg");   
		final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**.{class,txt}");
		try (final Stream<Path> stream = Files.list(path)) {
			stream.filter(pathMatcher::matches).forEach(System.out::println);
		}
		catch(IOException ioe){ioe.printStackTrace();}
    }
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.