Is there an easy way to go through an entire directory recursively to find all CSV files? In python, there's os.walk and I see that Java has FileVisitor, but I have no idea how to use it correctly (http://download.oracle.com/javase/tutorial/essential/io/walk.html).

What I want to do is something like this (pseudocode):

mainDirectory = r'C:\Downloads\MyFolder'
// Must work for directories within mainDirectory
for (file : mainDirectory){
    if (filename.find('csv') > -1){
        callMyMethod(filesrc)}
}

Can someone help me convert this into actual Java code? Thanks a lot!

Recommended Answers

All 2 Replies

for example

for (int i = 0, n = file; i < n; i++) {
   if (file[i].toString().contains(".cvs")) {...

Usable, but not bullet-proof. What in case that file is called like "myfile.cvs.txt"?
You could possibly remedy this with endsWith(String suffix) .

Nevertheless there are better solutions like Apache Commons FilenameUtils.getExtension(String filename)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.