• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Doubt in a method of File class

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
author
Posts: 23901
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 73289
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 73289
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}
}>
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic