- See Also:
- Top Examples, Source Code,
FileDialog.setFilenameFilter(java.io.FilenameFilter)
,
File
,
File.list(java.io.FilenameFilter)
boolean accept(File dir,
String name)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[61]Listing files with extension mpg
By Anonymous on 2005/04/13 09:28:30 Rate
import java.io.FilenameFilter;
public class MPGFileFilter implements FilenameFilter {
public boolean accept ( java.io.File f ) {
return f.getName ( ) .endsWith ( ".mpg" ) || f.getName ( ) .endsWith ( ".MPG" ) ;
}
}
public class ClassOne {
public void someMethod ( ) {
File directory = new File ( "C:\\temp" ) ;
File [ ] list = directory.listFiles ( new MPGFileFilter ( ) ) ;
}
}
[62]Directory listing
By Anonymous on 2004/07/19 14:30:32 Rate
//: DirList.java
// Displays directory listing
package c10;
import java.io.*;
public class DirList {
public static void main ( String [ ] args ) {
try {
File path = new File ( "." ) ;
String [ ] list;
if ( args.length == 0 )
list = path.list ( ) ;
else
list = path.list ( new DirFilter ( args [ 0 ] ) ) ;
for ( int i = 0; i < list.length; i++ )
System.out.println ( list [ i ] ) ;
} catch ( Exception e ) {
e.printStackTrace ( ) ;
}
}
}
class DirFilter implements FilenameFilter {
String afn;
DirFilter ( String afn ) { this.afn = afn; }
public boolean accept ( File dir, String name ) {
// Strip path information:
String f = new File ( name ) .getName ( ) ;
return f.indexOf ( afn ) != -1;
}
} ///:~
[1308]File filter will check to see if the file is really a file and not a directory
By Shaiful Nizam (zam_023 { at } yahoo { dot } com) on 2005/02/16 22:45:15 Rate
My filter will check to see if the file is really a file and not a directory.
public class XMLFileFilter implements FilenameFilter
{
public boolean accept ( File dir, String name )
{
boolean flag = false;
File file = new File ( dir,name ) ;
if ( file.isFile ( ) )
{
flag = name.endsWith ( ".xml" ) ;
}
return flag;
}
}
[1785]FileNameFilter
By any1 { dot } yahoo { dot } com on 2006/07/08 21:04:58 Rate
//Hey Shaiful Nizam. This is your simplified version... : )
public class XMLFileFilter implements FilenameFilter
{
public boolean accept ( File dir, String name )
{
if ( new File ( dir, name ) .isDirectory ( ) ) return false;
return name.endsWith ( ".xml" ) || name.endsWith ( ".XML" ) ;
}
}
[1987]
By s1w_ on 2008/12/06 06:25:17 Rate
fnames = new File ( dir ) .list ( new FilenameFilter ( ) {
public boolean accept ( File dir_, String file ) {
for ( String ex : new String [ ] { "jpg","jpeg","gif","png" } )
if ( file.toLowerCase ( ) .endsWith ( ex ) ) return true;
return false;
}
} ) ;