Search For Specific File Type (eg .txt)

I know how how to find files in a folder using Java but how do you search for specific file types. I am looking only include files with the extension .txt in my search.

[175 byte] By [paul_carrona] at [2007-11-27 10:21:30]
# 1

Apache Commons IO has a lot of nice file utilities, inluding listing files with wildwards.

SurfManNLa at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 2

Take a look here for an example on how to traverse a directory.

http://www.exampledepot.com/egs/java.io/TraverseTree.html

all you have to do after this is use String.endsWith(".txt"); once you have an array file names to process.

_helloWorld_a at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 3

> I know how how to find files in a folder using Java

> but how do you search for specific file types. I am

> looking only include files with the extension .txt in

> my search.

Use a custom FileFilter. Here's a little demo:

File aFolder = new File("/home/userx");

// get all textfiles from /home/userx

File[] textFiles = aFolder.listFiles(new FileFilter() {

public boolean accept(File file) {

return file.getName().endsWith(".txt") && file.isFile();

}

});

prometheuzza at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 4

Thanks guys.

paul_carrona at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 5

What if I wanted to read the endsWith value from an XML file?

paul_carrona at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 6

> What if I wanted to read the endsWith value from an XML file?

Then you adjust the example I posted earlier.

prometheuzza at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 7

Sorry, what I meant was that instead of actually typing ".txt" into endsWith, the value is read from an XML such as:

<file>

<file_details>

<starts_with>A</starts_with>

<extension>.txt</extension>

</file_details>

</file>

paul_carrona at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 8

Ah, then you'll have to parse the XML file:

http://java.sun.com/developer/Books/xmljava/ch03.pdf

prometheuzza at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...
# 9

Cheers. I already looked at that. Got a program that parses a XML and had a play about with it. Can print the contents of tags in my XML. Its just that im trying to parse my XML and have the contents of the starts_with tag to appear in startsWith(or a variable to which this has been assigned) and the contents of the extension tag to appear in endsWith. Totally clueless on how to go about it.

Below is my class. Would be greatful if you have any ideas.

import java.io.File;

public class FindLatestFile {

public static File getLatest(File thisDir){

long latestModDate = -1;

File latestFile = null;

File[] fileList = thisDir.listFiles();

for(int i=0; i < fileList.length; i++){

File file = fileList[i];

if (file.lastModified() > latestModDate & (file.getName().startsWith("A") & file.getName().endsWith(".txt"))) {

latestModDate = file.lastModified();

latestFile = file;

}

}

return latestFile;

}

}

paul_carrona at 2007-7-28 17:10:23 > top of Java-index,Java Essentials,New To Java...