GroupDocsGists
10/24/2017 - 1:12 PM

Program.java

public class Program {
    private final String[] extensions;

    public Program(String... extensions) {
        this.extensions = extensions;
    }

    public void execute(String inDirectory, String outExtension, IDoIt iDoIt) throws Exception {
        final File inFile = new File(inDirectory);
        System.out.println("Work directory: " + inFile.getAbsolutePath());
        final File[] files = inFile.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                for (String ext : extensions) {
                    if (name.endsWith(ext)) {
                        return true;
                    }
                }
                return false;
            }
        });
        for (int index = 0; files != null && index < files.length; index++) {
            File file = files[index];
            final String inPath = file.getAbsolutePath();
            final String name = file.getName();
            final String outBasePath = new File(file.getParent()).getAbsolutePath() + File.separator + name.replaceAll("\\.", "_");
            iDoIt.doIt(inPath, outBasePath, outExtension, outBasePath + outExtension, index);
        }
    }

    public static interface IDoIt {
        void doIt(String inPath, String outBasePath, String extension, String outPath, int fileIndex) throws Exception;
    }
}