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?
 
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
 
awking00Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.