0

If Folder Contain File then only Display that Directory name in Java. how can we do that . as of now i am Getting Main Folder and Sub Folder name. please Find snapshot for better understanding

public class GetChild {


    private static FileFilter onlyDirectories = new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    };
     public static void main(String[] args) {
        File currentDir = new File("E:\\Folder"); // current directory
        displayDirectoryContents(currentDir);
    }

    public static void displayDirectoryContents(File dir) {
        StringBuilder sb1 = new StringBuilder("");
       doDisplayDirectoryContents(dir, sb1);

    }

    private static void doDisplayDirectoryContents(File dir, StringBuilder sb1) {
        File[] files = dir.listFiles(onlyDirectories);
        for (File file : files) {
            try {
                System.out.println("file.getCanonicalPath()===>" + file.getCanonicalPath());
                doDisplayDirectoryContents(file, sb1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      }
}

enter image description here

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Is this Java 7+? If yes, any reason why you don't use java.nio.file? – fge Jul 03 '15 at 08:30
  • i am Using Java 7 but i have perform more task with same code but initially this code creates problem . i want only SubFolder name thos who Contain any Files . better you see snapshot . Thanks –  Jul 03 '15 at 08:35

2 Answers2

2

You can use a combination of FileFilter and File.listFiles() to achieve this.

FileFilter filter = new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        return pathname.isFile();
    }
};
if (file.listFiles(filter).length > 0) {
    System.out.println("file.getCanonicalPath()===>" + file.getCanonicalPath());
else
    continue;
Codebender
  • 14,221
  • 7
  • 48
  • 85
  • file.getCanonicalPath() give Same Folder Directory. please see my code once –  Jul 03 '15 at 08:44
  • @Jordi Castilla: :Your Code Return Only Sub Folder . i want to Access Some More Folder Inside Main Folder . if i want to access Folder and Sub Folder so only option is recursion . better you tell me if Folder Contain File then Show else ignore it . see my code it works Fine but it shows some Folder name those who does Not Contain any File . please Check once my Code and Snapshot . Thanks –  Jul 05 '15 at 11:34
1

You are doing things in a really complicated way... hope this clarifies...

String filename = "myFile";
String folderPath = "E:\\Folder";

File folder = new File(folderPath);
File file = new File(folderPath, filename);

if (folder.exists() && folder.isDirectory() && file.exists() && file.isFile()) {
    // show file
} else {
    // error
}

If what you want is to explore all folders of a given one and see if there is files inside what you must use is recursion. Check mkyong and SO sollutions...

UPDATE: here you have my sollution uising recursion and printing ONLY FOLDERS THAT CONTAINS FILES (NOT FOLDERS).

public static void main(String[] args) throws Exception{
        String folderPath = "C:\\Android";

        File folder = new File(folderPath);

        if (folder.exists())
            read(folder);
    }

    public static void read(File folder) {
        if (!folder.canRead() || folder.isFile()) return;
        File[] files = folder.listFiles();
        if (files.length < 1) return;
        boolean hasFile  = false;
        for (File f : files) {
            if (f.isDirectory())
                read(f);
            if (f.isFile())
                hasFile = true;
        }

        if (hasFile) System.out.println(folder.getAbsolutePath());
    }
}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Still i am getting same Path . –  Jul 03 '15 at 08:41
  • i want to print only Folder Name those who Contain File . i dont want to list of Files . here i am getting Some Folder Name Those Who not Contain File . see snapshot or code –  Jul 03 '15 at 09:01
  • Check updated Code with Snapshot For Better understand –  Jul 03 '15 at 09:15