search Dir by file name - Java File Path IO

Java examples for File Path IO:Directory Search

Description

search Dir by file name

Demo Code


//package com.java2s;

import java.io.File;

public class Main {
    public static void main(String[] argv) throws Exception {
        String path = "java2s.com";
        String target = "java2s.com";
        searchDir(path, target);//from w  w  w  .j  av a  2 s .c  om
    }

    public static void searchDir(String path, String target) {
        File f = new File(path);
        if (path != null) {
            if (f.isDirectory()) {
                File[] fileArray = f.listFiles();
                if (fileArray != null) {
                    for (File file : fileArray) {
                        searchDir(file.getPath(), target); //????
                    }
                }
            } else {
                if (f.getName().contains(target)) {
                    System.out.println(f);
                }
            }
        }
    }
}

Related Tutorials