Find a folder in Java I am making a website which should host a lot of PDFs. These PDFs are stored on the HDD. There are DossierIds in the database which correspondent with the folder name. In these folders there is 1 layer of folders and in those layers are the PDFs.

To make the structure clear:



- ABC
    - Dossier1
        -File1.pdf
        -File2.pdf
    - Dossier2
        -File1.pdf
        -File2.pdf
        -File3.pdf
-DEF
    - Dossier1
        -File1.pdf
    - Dossier2
        -File1.pdf
        -File2.pdf
    - Dossier3
        -File1.pdf


The layer which contains the most folders it the root layer (so in the example: ABC, DEF, etc).

I wrote the following code but I'm not sure if it's optimal and if it can handle concurrent access:



public List&lt;Folder&gt; getFolders(String dossierId) {
    //Going right to the correct folder. Not sure how heavy this operation is.
    File file = new File(String.format("%s\\%s", 
        propertyService.getProperties().get("folderLocation"), dossierId));

    //Retrieving all folders in that directory.
    File[] directories = file.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return new File(dir, name).isDirectory();
        }
    });

    List&lt;Folder&gt; folders = new ArrayList&lt;&gt;();

    //Going through the folders to check for files
    for (File f : directories) {
        File folder = new File(f.getPath());
        File[] listOfFiles = folder.listFiles();
        List&lt;String&gt; fileNames = new ArrayList&lt;&gt;();
        for (File childfile : listOfFiles) {
            fileNames.add(childfile.getName());
        }

        folders.add(new Folder(f.getName(), fileNames));
    }

    return folders;
}
The code is not optimal. Furthermore, it breaks the single responsibility principle since the method can be broken down to smaller, tighter pieces. For example, the for loop can and should be put in its own method. what this will give you? consider what if in the future you decide to change the folder hierarchy (perhaps add new folder layer or put some files in the root folder etc). what you need is a separate method that can travel along folder tree of any depth and gather the files (perhaps using recurssion?) 

Regarding performance:


You call propertyService.getProperties().get("folderLocation") every time the method is called. However, it looks to me like this value is not changed with every call. 
String.format is an expensive call since the format is parsed for tokens. Replace that with simple concatanation. and use forward slash - it works for *nix AND Windows systems (the JVM handles it)
You are using pre-java-7 IO library. Java 7 introduced a new, simpler, better, safer, faster library: java.nio. use it. 
Each time the method is called, you scan the file system. Depending on the total size of the PDF library - you may consider loading the whole structure into memory. the java.nio has utilities that can alert your program when changes ocur in the file system (like new file or deleted file etc) so you can keep your in-memory tree in sync with the actual file system.
If pre-loading the file system structure is not an option, consider implementing a cache system where you store previous method calls (store an input and its output) and then you can return output for previouisly called input. You will need a cache system like LRU map. There are readily available 3rd party libraries for that.