Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.selesse.jxlint.utils;
 
 
 import java.io.File;
 import java.net.URI;
public class FileUtils {
    private static Predicate<FileIS_FILE = new Predicate<File>() {
        @Override public boolean apply(File file) {
            return file != null && file.isFile();
        }
    };
    private static Function<FileStringGET_NAME = new Function<FileString>() {
        @Override public String apply(File file) {
            return file == null ? null : file.getName();
        }
    };
    private static List<FileallFilesSatisfyingPredicate(File rootDirPredicate<Stringpredicate) {
        return Files.fileTreeTraverser()
            .preOrderTraversal(rootDir)
            .filter()
            .filter(Predicates.compose(predicate))
            .toList();
    }

    
Recursively get all files in a directory.
    public static List<FileallFiles(File rootDir) {
        return allFilesSatisfyingPredicate(rootDir, Predicates.<String>alwaysTrue());
    }

    
Return all files in a directory, recursively, that have the given extension. The extension does not include the period, i.e. "txt" would match "file.txt".
    public static List<FileallFilesWithExtension(File rootDirfinal String extension) {
        return allFilesSatisfyingPredicate(rootDirnew Predicate<String>() {
            @Override public boolean apply(String name) {
                return name != null && Files.getFileExtension(name).equalsIgnoreCase(extension);
            }
        });
    }

    
Return all files in a directory, recursively, that have the given filename.
    public static List<FileallFilesWithFilename(File rootDirString filename) {
        return allFilesSatisfyingPredicate(rootDir, Predicates.equalTo(filename));
    }

    
Return all files in a directory, recursively, that match a given regex.
    public static List<FileallFilesMatching(File rootDirfinal String regex) {
        return allFilesSatisfyingPredicate(rootDirnew Predicate<String>() {
            @Override public boolean apply(String name) {
                return name != null && name.matches(regex);
            }
        });
    }

    
Return all files in a directory, recursively, that contain a certain substring in their filename.
    public static List<FileallFilesContaining(File rootDirfinal String substring) {
        return allFilesSatisfyingPredicate(rootDirnew Predicate<String>() {
            @Override public boolean apply(String name) {
                return name != null && name.contains(substring);
            }
        });
    }

    
Normalize the input file. This helps when printing paths. i.e. "java -jar jxlint.jar ." would print "/home/alex/git/jxlint/./text.txt"
    public static File normalizeFile(File file) {
        URI uri = file.toURI();
        uri = uri.normalize();
        String normalizedPath = uri.getPath();
        return new File(normalizedPath);
    }
    public static String getRelativePath(File parentFile child) {
        return parent.toURI().relativize(child.toURI()).getPath();
    }
New to GrepCode? Check out our FAQ X