Swaminathan K
asked on
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);
}
}
}
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);
}
}
}
What's the code for the class fileObj?
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])
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])
ASKER
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?
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?
ASKER
Hi The code is there in the question itself.
The extn parameter contains only the extension of the file, not the entire filename.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();}
}
}