Aide mémoire : Exploration de dossier

Aide mémoire (Développement) / Uncategorized
Petit aide mémoire pour explorer le contenu d’un dossier de manière récursive.

1. Java 6
2. Java 7
3. Sources

1. Java 6

    public void run() {
        File f = new File(fileOrFolderPath);
        if (f.isDirectory()) {
            parseFolder(f);
        } else {
            parseFile(f);
        }
    }

    /**
     * Recursively parse given folder.
     */
    protected void parseFolder(File directory) {
        doAtStartOfEachFolder(directory);
        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
                parseFolder(file);
            } else {
                parseFile(file);
            }
        }
        doAtEndOfEachFolder(directory);
    }

    /**
     * Read given file, line by line.
     */
    protected void parseFile(File file) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            doAtStartOfEachFile(file);
            String line;
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while ((line = br.readLine()) != null) {
                parseLigne(line);
                // because it will be too fast 				
                Thread.sleep(timeout);
            }
        } catch (Exception e) {
            LOGGER.error("Parsing File failed : " + e.getMessage());
        } finally {
            try {
                if (fr != null) {
                    fr.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (Exception ex) {
                LOGGER.error("Can't close bufferReader: " + ex.getMessage());
            }
            doAtEndOfEachFile(file);
        }
    }

    /**
     * Send given line to consumer. Override it if you need to do specific modification before sending it to consumer.
     */
    protected void parseLigne(String line) {
        this.getConsumer().consume(line);
    }

    /**
     * Specific code to do before reading a file.
     */
    protected void doAtStartOfEachFile(File file) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Read file: " + file.getName());
        }
    }

    /**
     * Specific code to do when file as no more line to read.
     */
    protected void doAtEndOfEachFile(File file) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("end of file: " + file.getName());
        }
    }

    /**
     * Specific code to do before reading through a folder.
     */
    protected void doAtStartOfEachFolder(File folder) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Read through folder: " + folder.getName());
        }
    }

    /**
     * Specific code to do when all files/sub-folder are read.
     */
    protected void doAtEndOfEachFolder(File folder) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("No more file to read in folder: " + folder.getName());
        }
    }

2. Java 7

    public static void main(String[] args) {
        for (String s : fileList("D:\\___DL\\_new\\bd\\")) {
            System.out.println(s);
        }
    }

    public static List fileList(String directory) {
        List fileNames = new ArrayList();
        try (DirectoryStream directoryStream = Files.newDirectoryStream(Paths.get(directory))) {
            for (Path path : directoryStream) {
                File f = path.toFile();
                if (f.isDirectory()) {
                    fileNames.addAll(fileList(f.getAbsolutePath()));
                } else {
                    fileNames.add(path.toString());
                }
            }
        } catch (IOException ex) {
        }
        return fileNames;
    }

3. Sources
http://www.adam-bien.com/roller/abien/entry/listing_directory_contents_with_jdk
http://www.drdobbs.com/jvm/java-se-7-new-file-io/231600403