Example usage for java.io File list

List of usage examples for java.io File list

Introduction

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

Prototype

public String[] list() 

Source Link

Document

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

Usage

From source file:com.depas.utils.FileUtils.java

/** Deletes all directory and all files/directories in it */
public static boolean deleteDirectory(File dir) {

    if (dir == null) {
        return false;
    }//from   ww  w.  j a v a2s. c  o  m
    if (dir.isDirectory()) {
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDirectory(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
    }
    // The directory is now empty so delete it
    return dir.delete();
}

From source file:com.baidu.rigel.biplatform.tesseract.util.FileUtils.java

/**
 * ?// w w w. j  a  v a2  s  .  c  om
 * 
 * @param oldPath
 *            String  c:/fqf
 * @param newPath
 *            String ?? f:/fqf/ff
 * @return boolean
 * @throws Exception
 */
public static void copyFolder(String oldPath, String newPath) throws Exception {
    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_BEGIN, "copyFolder",
            "[oldPath:" + oldPath + "][newPath:+" + newPath + "]"));
    try {

        File newPathFile = new File(newPath);
        newPathFile.mkdirs();

        File oldPathFile = new File(oldPath);
        String[] file = oldPathFile.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }

            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(
                        newPath + File.separator + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                // ?
                copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]);
            }
        }
    } catch (Exception e) {
        LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_EXCEPTION, "copyFolder",
                "[oldPath:" + oldPath + "][newPath:+" + newPath + "]"));
        throw e;

    }

    LOGGER.info(String.format(LogInfoConstants.INFO_PATTERN_FUNCTION_END, "copyFolder",
            "[oldPath:" + oldPath + "][newPath:+" + newPath + "]"));

}

From source file:it.feio.android.omninotes.utils.StorageManager.java

public static boolean copyDirectory(File sourceLocation, File targetLocation) {
    boolean res = true;

    // If target is a directory the method will be iterated
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdirs();/*from  w w w .  j  a  va2s.  c  o m*/
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < sourceLocation.listFiles().length; i++) {
            res = res && copyDirectory(new File(sourceLocation, children[i]),
                    new File(targetLocation, children[i]));
        }

        // Otherwise a file copy will be performed
    } else {
        try {
            res = res && copyFile(new FileInputStream(sourceLocation), new FileOutputStream(targetLocation));
        } catch (FileNotFoundException e) {
            Log.e(Constants.TAG, "Error copying directory", e);
            res = false;
        }
    }
    return res;
}

From source file:com.norconex.commons.lang.file.FileUtil.java

/**
 * Visits all files (and only files) under a directory, including 
 * sub-directories.//from w  ww .j a  v  a  2 s .  com
 * @param dir the directory
 * @param visitor the visitor
 * @param filter an optional filter to restrict the files being visited 
 */
public static void visitAllFiles(File dir, IFileVisitor visitor, FileFilter filter) {
    if (!dir.exists()) {
        return;
    } else if (dir.isDirectory()) {
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                visitAllFiles(new File(dir, children[i]), visitor, filter);
            }
        }
    } else if (filter == null || filter.accept(dir)) {
        visitor.visit(dir);
    }
}

From source file:fi.mikuz.boarder.util.FileProcessor.java

private static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();/*from   w  w w .  j  av  a 2 s  .  c  o  m*/
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else {
        copyFile(sourceLocation, targetLocation);
    }
}

From source file:com.wavemaker.common.util.IOUtils.java

public static long getMostRecentModificationTime(File f, String... excludes) {
    if (!f.exists()) {
        throw new IllegalArgumentException("File doesn't exist: " + f);
    }//from w w  w. j av  a2  s . c o  m

    if (f.isFile()) {
        return f.lastModified();
    } else {
        long rtn = -1;
        for (String s : f.list()) {
            for (String exclude : excludes) {
                if (s.equals(exclude)) {
                    continue;
                }
            }
            File f2 = new File(f, s);
            long l = getMostRecentModificationTime(f2);
            if (l > rtn) {
                rtn = l;
            }
        }
        return rtn;
    }
}

From source file:gate.util.Files.java

/** Recursively remove a directory <B>even if it contains other files
  * or directories</B>. Returns true when the directory and all its
  * contents are successfully removed, else false.
  *///  w w  w .  ja v  a 2s.  c  om
public static boolean rmdir(File dir) {
    if (dir == null || !dir.isDirectory()) // only delete directories
        return false;

    // list all the members of the dir
    String[] members = dir.list();

    // return value indicating success or failure
    boolean succeeded = true;

    // for each member, if is dir then recursively delete; if file then delete
    for (int i = 0; i < members.length; i++) {
        File member = new File(dir, members[i]);

        if (member.isFile()) {
            if (!member.delete())
                succeeded = false;
        } else {
            if (!Files.rmdir(member))
                succeeded = false;
        }
    }

    // delete the directory itself
    dir.delete();

    // return status value
    return succeeded;
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.QcliveAbstractBaseIntegrationTest.java

/**
 * Check all files under the given path and delete them if they are not in the given list of valid paths
 *
 * @param path the path where to start checking the paths validity (this one included)
 * @param validPaths the list of valid paths
 *//*from  w w  w . jav a  2s.c o m*/
private static void cleanPath(final String path, final List<String> validPaths) {

    // Delete path if not valid
    final File file = new File(path);

    if (!validPaths.contains(file.getAbsolutePath())) {

        final boolean isDeleted = FileUtil.deleteDir(file);

        if (!isDeleted) {
            fail("Could not delete file '" + file.getAbsolutePath() + "'");
        }
    }

    // Recursively clean children (don't try this at home)
    if (file.isDirectory()) {
        final String[] children = file.list();
        for (final String child : children) {
            cleanPath(path + File.separator + child, validPaths);
        }
    }
}

From source file:com.norconex.commons.lang.file.FileUtil.java

/**
 * Visits only directories under a directory.
 * @param dir the directory/*from ww w .  j  a va  2s  .com*/
 * @param visitor the visitor
 */
public static void visitAllDirs(File dir, IFileVisitor visitor) {
    if (!dir.exists()) {
        return;
    } else if (dir.isDirectory()) {
        visitor.visit(dir);
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                visitAllDirs(new File(dir, children[i]), visitor);
            }
        }
    }
}

From source file:jeeves.utils.BinaryFile.java

/**
 * Copy a directory from one location to another.
 * //from   www  .  ja  va 2  s  .c  o m
 * @param sourceLocation
 * @param targetLocation
 * @throws IOException
 */
public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.mkdirs() && !targetLocation.exists()) {
            throw new IOException("Unable to create target directory " + targetLocation);
        }

        String[] children = sourceLocation.list();
        for (int i = 0; i < children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(sourceLocation);
            out = new FileOutputStream(targetLocation);
            copy(in, out, false, false);
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }
            if (out != null) {
                IOUtils.closeQuietly(out);
            }
        }
    }
}