|
How to get the list of specific file types in a directory |
|
|
java.io package gives the facility of playing with files and directory.
Example below lists files from a directory. User inputs directory path and
the file extension.
import java.io.*;
class testFiles
{
public static void main(String[] args)
{
if (args.length != 2)
{
System.out.println("Usage: java testFiles [directory path] " +
"\"[file extension]\"");
return;
}
try
{
String directory = args[0];
String ext = args[1];
testFiles list = new testFiles( );
list.getFiles(directory,ext);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void getFiles(String directory, String ext)
{
try
{
File f = new File(directory);
boolean flag = f.isDirectory();
if(flag)
{
File fs[] = f.listFiles();
for(int i=0;i<fs.length;i++)
{
if(!fs[i].isDirectory())
{
String filename = fs[i].getName();
if(filename.endsWith(ext.trim()))
System.out.println(filename);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.