Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in a method of File class  RSS feed

 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me the meaning and purpose of "filter" in the method below. Can this be used to search for desired files using a "filter" like : "list files that begin with only B or B" etc...(i need such a feature for one of my kiddie projects)

File[] listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

-source SUN docs

 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote:Can this be used to search for desired files using a "filter" like : "list files that begin with only B or B" etc...


yes
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Rahul Sudip Bose wrote:Can this be used to search for desired files using a "filter" like : "list files that begin with only B or B" etc...


yes


can anyone give me some sample code OR basic guidance that will enable me to,say, list files that contain - or . in their file name.

 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to learn your way round the API, where you find useful stuff like this page. It even has the sort of example you were asking about on.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You need to learn your way round the API, where you find useful stuff like this page. It even has the sort of example you were asking about on.


this uses swing/GUI. can we do it in console ? I need to do this for a bigger kiddie project that i am working on.

 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to extend/implement the interface- FileFilter and override the accept method. It would work the same way for the console output as well. Just that you dont use JFileChooser for file listing instead explore the File API.
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Sudip Bose wrote: . . . can we do it in console ? . . .
Yes. You may need to pass null as an argument for the location of the file chooser.
 
sandy sean
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul !!!

you may use following code:-

import java.io.*;

class test1 implements FileFilter{

public boolean accept(File path){

int index=path.toString().lastIndexOf(File.separator);
String filename=path.toString().substring(index+1);

if(path.isFile() && filename.startsWith("B")){
System.out.println("2");
return true;
}

return false;

}

public static void main(String args[]){

String path="/" //my root drive
File f= new File(path);
test1 t= new test1();
File s[]=f.listFiles(t);
System.out.println("SELCETED FILES");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
}>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!