Example usage for java.io File listFiles

List of usage examples for java.io File listFiles

Introduction

In this page you can find the example usage for java.io File listFiles.

Prototype

public File[] listFiles() 

Source Link

Document

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

Usage

From source file:Main.java

/**
 * Get files of directory you passed. The files contains files of passed directory's sub directory by recursive exploring.
 *//* w  w w .j  a v  a2  s . c o  m*/
public static List<File> getAllFiles(File directory) {
    final List<File> fileList = new LinkedList<File>();
    final File[] files = directory.listFiles();

    if (files == null)
        return fileList;
    for (File file : files) {
        if (file.isDirectory())
            fileList.addAll(getAllFiles(file));
        else
            fileList.add(file);
    }
    return fileList;
}

From source file:ReflectionUtil.java

private static void getPackageNamesFromDir(File base, File dir, List<String> pkgs) {
    boolean foundClass = false;
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) {
            getPackageNamesFromDir(base, file, pkgs);
        } else if (!foundClass && file.getName().endsWith(".class")) {
            foundClass = true;/*from   w  w  w. j  a v  a 2 s .  c o  m*/
            String pkg = "";
            file = dir;
            while (!file.equals(base)) {
                if (!"".equals(pkg)) {
                    pkg = "." + pkg;
                }
                pkg = file.getName() + pkg;
                file = file.getParentFile();
            }
            if (!pkgs.contains(pkg)) {
                pkgs.add(pkg);
            }
        }
    }
}

From source file:Main.java

public static void deleteFile(String fileName) {
    File file = new File(fileName);
    if (file.exists()) {
        if (file.isDirectory()) {
            if (file.listFiles().length == 0) {
                file.delete();//from   ww w.  jav a 2s.c  om
            } else {
                for (File f : file.listFiles()) {
                    if (f.isDirectory()) {
                        deleteFile(f.getPath());
                    } else {
                        f.delete();
                    }
                }
                file.delete();
            }
        } else {
            file.delete();
        }
    }
}

From source file:Main.java

/**
 * Clears any leftover photos in our temporary folder.
 *///from  w  ww.  j a  v a 2  s . c  o m
public static void clearPhotos() {
    File tempDir = Environment.getExternalStorageDirectory();
    tempDir = new File(tempDir.getAbsolutePath() + "/bv-temp/");
    if (tempDir.listFiles() != null) {
        for (File photo : tempDir.listFiles()) {
            photo.delete();
        }
    }
}

From source file:Main.java

public static void deleteFile(String filePath) {
    File file = new File(filePath);
    if (file.exists()) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteFile(files[i].getPath());
                } else {
                    files[i].delete();//from   w ww  . ja v  a2s .c  o m
                }
            }
        }
        file.delete();
    }
}

From source file:com.thoughtworks.go.util.FileDigester.java

public static String md5DigestOfFolderContent(File directory) throws IOException {
    File[] files = directory.listFiles();
    Arrays.sort(files, NameFileComparator.NAME_COMPARATOR);
    StringBuilder md5 = new StringBuilder();
    for (File file : files) {
        if (file.isDirectory())
            md5.append(md5DigestOfFolderContent(file));
        else/*ww  w  . j a va  2  s .  co m*/
            md5.append(file.getName() + md5DigestOfFile(file));
    }
    return md5DigestOfStream(new ByteArrayInputStream(md5.toString().getBytes(CharEncoding.UTF_8)));
}

From source file:Main.java

public static boolean deleteFolderFile(String filePath, boolean deleteThisPath) {
    if (!TextUtils.isEmpty(filePath)) {
        try {/*from   www. j  a  v  a  2s  .c om*/
            File file = new File(filePath);
            if (file.isDirectory()) {
                File files[] = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    deleteFolderFile(files[i].getAbsolutePath(), true);
                }
            }
            if (deleteThisPath) {
                if (!file.isDirectory()) {
                    file.delete();
                } else {
                    if (file.listFiles().length == 0) {
                        file.delete();
                    }
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}

From source file:com.clothcat.hpoolauto.HtmlUploader.java

/**
 * Upload the html via FTP//from w ww.j a v  a2s. c  o  m
 */
public static void uploadHtml() {
    FTPClient ftpc = new FTPClient();
    FTPClientConfig conf = new FTPClientConfig();
    ftpc.configure(conf);
    try {
        ftpc.connect(Constants.FTP_HOSTNAME);
        ftpc.login(Constants.FTP_USERNAME, Constants.FTP_PASSWORD);
        File dir = new File(Constants.HTML_FILEPATH);
        File[] files = dir.listFiles();
        ftpc.changeWorkingDirectory("/htdocs");
        for (File f : files) {
            HLogger.log(Level.FINEST, "Uploading file: " + f.getName());
            FileInputStream fis = new FileInputStream(f);
            ftpc.storeFile(f.getName(), fis);
        }
        ftpc.logout();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (ftpc.isConnected()) {
            try {
                ftpc.disconnect();
            } catch (IOException ioe) {
            }
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.source.SVN.java

/**
 * Ensures that all files inside the specified directory has consistent new lines.
 * @param dir the directory to recursively ensure all contained files have consistent new lines
 * @throws IOException if an exception happens
 *//*from  ww  w.j ava 2  s  . c  o m*/
public static void consistentNewlines(final File dir) throws IOException {
    for (final File f : dir.listFiles()) {
        if (f.isDirectory()) {
            if (!".svn".equals(f.getName())) {
                consistentNewlines(f);
            }
        } else {
            final String fileName = f.getName().toLowerCase(Locale.ROOT);
            for (final String extension : EOL_EXTENSIONS_) {
                if (fileName.endsWith(extension)) {
                    FileUtils.writeLines(f, FileUtils.readLines(f));
                    break;
                }
            }
        }
    }
}

From source file:Main.java

public static Vector getSignaturesForFile(File originalFile) {
    File sigDir = getSignatureDirectoryForFile(originalFile);

    File[] filesAvailable = sigDir.listFiles();
    Vector signatureFiles = new Vector();

    if (filesAvailable != null) {
        for (int i = 0; i < filesAvailable.length; i++) {
            File file = filesAvailable[i];
            if (isMatchingSigFile(originalFile, file)) {
                signatureFiles.add(file);
            }//  ww w . ja  v  a 2s  . c om
        }
    }
    return signatureFiles;
}