• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FilenameFilter query: How to filter for "*.txt" or *.*(for all) or any other extension with asterisk

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am new to java.io.FilenameFilter class.
My requirement is to take directory or file path from console and filter it to display the list of files with specified extensions.
for example: If user enters following path "C:\SampleDirectory\*.txt" then program should be able to display all text files under SampleDirectory.

I have implemented following code but this will not detect "C:\SampleDirectory\*.txt" as a proper file name.
So how "accept" method will work to filter all .txt files if user input is given like *.txt from console.


accept(File obj,String FileName) method which takes one argument of File type & name of files.
It returns true if FileName is present in path identified by obj. What if I give asterisk for all files ?


Please help

Thanks in Advance
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pratibha patel wrote:I have implemented following code but this will not detect "C:\SampleDirectory\*.txt" as a proper file name.


What do you mean, "will not detect"? Does it just ignore it? Does it give you an error? Please TellTheDetails (←click).

Also: Does this mean that you actually have a file called "*.txt"? Because that's just asking for trouble - and not just in Java - although I certainly agree that it should appear.

Off the top of my head I can't see anything major wrong with your code. You could replace the entire accept() method with:
return name.toLowerCase().endsWith(".txt");
though.

I actually have a question for you (or anyone else who knows): What's the difference between File.list() and File.listFiles()? They seem to have duplicated an entire portion of the API in version 1.2, but nothing leaps out at me from the documentation as to why.

Winston
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:What's the difference between File.list() and File.listFiles()?


One of them returns String[] and the other File[]
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is aesterisk supposed to be commented out?
Don't use an if-else to return boolean values. Simply write
return fileName.toLowerCase().endsWith(".txt");
See §10.5.2 here.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using File and FileNameFilter?
Now that NIO2 is available, those are regareded as legacy classes. Look in the Java Tutorials. Start with the Path and Paths classes.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:One of them returns String[] and the other File[]


Doh-h-h!!!

Sometimes you can't see the woods for the trees.

Winston
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

Thanks for your time and kind reply.
I think I have not clearly mentioned the actual problem I am facing.
Let me explain you my query once again.

What I am expecting: To display list of all files under the specified directory (I know it sounds so simple)

What should be the user input: Directory path should be provided by user from console.

for example: C:\SampleDirectory\*.txt

If I provide directory path as above (with asterisk(*.txt)) to display all txt files under \SampleDirectory. Then my code fails here.
for example


Above code variable inputDir ="C:\SampleDirectory\*.txt".
And if a do any operation on this inputDir it fails to operate for files or directories.

for example below code will return false and that is well understood because "*.txt" is not a file name.

So here the problem arises, now is there any way to detect it as a wildcard extension or not a file, If I do any further File operations like filtering for all txt files if user provide *.txt and not hardcoded for file filteration like

I hope you understand my query. Please help.

Thanks in Advance.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what you mean is, you want the directory before the * and the extension after the *?
Can you split the path on the *, on the last . and on the last / ?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, if you use a regular expression, some of those characters are meta‑characters.
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell for your concern and reply.

Campbell Ritchie wrote:So what you mean is, you want the directory before the * and the extension after the *?
Can you split the path on the *, on the last . and on the last / ?


Yes. You got me right, I want to access all files with extension after * under the directory before *.
Also I tried to split the following path: C:\SampleDirectory\*.txt

Above code returns "txt" string that is fine.
But now how to filter for all txt files under the specified directory(eg. under SampleDirectory) ?
What method should I use to filter files under such scenerio ?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There must be an easier way to do that. What about finding the last index of dot and returning the substring following that?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or just use this.
 
pratibha patel
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There must be an easier way to do that. What about finding the last index of dot and returning the substring following that?


Thanks a lot for the hint. This resolved my problem
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package loc;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public class TestLOC {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String folder = "D:/Personal";

File dir = new File(folder);
FilenameFilter f = new FilenameFilter() {
private String extn;
@Override
public boolean accept(File dir, String ext) {
//System.out.println("fh ===" +ext);
if(!ext.contains(".") || ext.contains(".tif")){
// TODO Auto-generated method stub
return true;
}else{
return false;
}
}

};


f.accept(dir, ".tif");

getLOC(dir, f);
}

private static void getLOC(File dir, FilenameFilter fl) {
// TODO Auto-generated method stub

if(dir.isFile()){
System.out.println(dir);
}else{
//System.out.println("Name ===== " + dir);
File[] f = dir.listFiles(fl);

for(int i=0;i< f.length; i++){
getLOC(f[i], fl);
}
}
}
}
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please always use code tags, and remove unwanted // comments. That will improve legibility no end.
 
reply
    Bookmark Topic Watch Topic
  • New Topic