Performs Maven-style filtering on a directory. - Java File Path IO

Java examples for File Path IO:Directory Search

Description

Performs Maven-style filtering on a directory.

Demo Code


//package com.java2s;
import java.io.File;
import java.util.Properties;

public class Main {
    /**//from w  w  w .  j  ava2  s  . co m
     * Performs Maven-style filtering on a directory. Allows filenames to be dynamically renamed, based on properties.
     * 
     * @param directory
     *            Directory to scan
     * @param properties
     *            List of properties used by the target Maven project
     */
    public static void doFilenameFiltering(final File directory,
            final Properties properties) {
        for (final File file : directory.listFiles()) {
            if (file.isDirectory()) {
                doFilenameFiltering(file, properties);
            } else {
                String fileName = file.getName();
                int i = fileName.indexOf("${");
                while (i >= 0) {
                    final String property = fileName.substring(i + 2,
                            fileName.indexOf('}', i));
                    if (properties.containsKey(property)) {
                        final String value = properties
                                .getProperty(property);
                        fileName = fileName.replaceAll("\\$\\{" + property
                                + "\\}", value);
                    }
                    i = fileName.indexOf("${", i + 1);
                }
                if (!fileName.equals(file.getName())) {
                    file.renameTo(new File(file.getParentFile(), fileName));
                }
            }
        }
    }
}

Related Tutorials