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.bb.extensions.plugin.unittests.internal.utilities.FileUtils.java

/**
 * Delete the directory and all files underneath.
 * //  w  w  w . j a v a  2 s .c  om
 * @param dir
 *            The directory to delete including all its children
 * @return True if the directory was deleted successfully false otherwise
 */
public static boolean deleteDirectoryRecursive(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDirectoryRecursive(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so delete it
    return dir.delete();
}

From source file:forge.CardStorageReader.java

public static List<File> collectCardFiles(final List<File> accumulator, final File startDir) {
    final String[] list = startDir.list();
    for (final String filename : list) {
        final File entry = new File(startDir, filename);

        if (!entry.isDirectory()) {
            if (entry.getName().endsWith(CardStorageReader.CARD_FILE_DOT_EXTENSION)) {
                accumulator.add(entry);/*from  w  ww  .  j av a  2s.co  m*/
            }
            continue;
        }
        if (filename.startsWith(".")) {
            continue;
        }

        collectCardFiles(accumulator, entry);
    }
    return accumulator;
}

From source file:Delete.java

/**
 * The static method that does the deletion. Invoked by main(), and designed
 * for use by other programs as well. It first makes sure that the specified
 * file or directory is deleteable before attempting to delete it. If there is
 * a problem, it throws an IllegalArgumentException.
 *//* w w w  .  ja  v  a  2  s  .c o m*/
public static void delete(String filename) {
    // Create a File object to represent the filename
    File f = new File(filename);

    // Make sure the file or directory exists and isn't write protected
    if (!f.exists())
        fail("Delete: no such file or directory: " + filename);
    if (!f.canWrite())
        fail("Delete: write protected: " + filename);

    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
        String[] files = f.list();
        if (files.length > 0)
            fail("Delete: directory not empty: " + filename);
    }

    // If we passed all the tests, then attempt to delete it
    boolean success = f.delete();

    // And throw an exception if it didn't work for some (unknown) reason.
    // For example, because of a bug with Java 1.1.1 on Linux,
    // directory deletion always fails
    if (!success)
        fail("Delete: deletion failed");
}

From source file:com.dnielfe.manager.utils.SimpleUtils.java

private static void search_file(String dir, String fileName, ArrayList<String> n) {
    File root_dir = new File(dir);
    String[] list = root_dir.list();

    if (list != null && root_dir.canRead()) {
        int len = list.length;

        for (int i = 0; i < len; i++) {
            File check = new File(dir + "/" + list[i]);
            String name = check.getName();

            if (check.isFile() && name.toLowerCase().contains(fileName.toLowerCase())) {
                n.add(check.getPath());//www .  j  a  va 2  s. c  o  m
            } else if (check.isDirectory()) {
                if (name.toLowerCase().contains(fileName.toLowerCase())) {
                    n.add(check.getPath());

                } else if (check.canRead() && !dir.equals("/"))
                    search_file(check.getAbsolutePath(), fileName, n);
            }
        }
    }
}

From source file:com.github.blindpirate.gogradle.util.IOUtils.java

public static List<String> safeList(File dir) {
    String[] files = dir.list();
    return files == null ? Collections.emptyList() : Arrays.asList(files);
}

From source file:Main.java

/*************************************************************************************************
 * Copies the directory to the destination
 * //  ww  w .ja  v a2 s.c  om
 */
public static void Copy(String srcPath, String dstPath) {
    File srcFile = new File(srcPath);
    File dstFile = new File(dstPath);
    String delim = new String("/");

    if (!srcFile.exists()) {
        Log.e("CopyDir", "Source does not exist!");
        return;
    }
    if (srcFile.isDirectory()) {
        dstFile.mkdirs();
        String files[] = srcFile.list();
        for (int i = 0; i < files.length; ++i) {
            Copy(srcPath + delim + files[i], dstPath + delim + files[i]);
        }
    } else {
        CopyFile(srcPath, dstPath);
    }
}

From source file:com.zimbra.cs.service.util.ItemDataFile.java

static void addDir(File f, String topdir, Set<MailItem.Type> types, TarOutputStream tos) throws IOException {
    String path = f.getPath();/*from   ww w.j a va  2 s  .c  om*/
    String[] all = f.list();
    List<File> dirs = new ArrayList<File>();
    List<File> files = new ArrayList<File>();

    Arrays.sort(all);
    for (String file : all) {
        File subf = new File(path + File.separator + file);

        if (subf.getName().equals("Tags") && path.equals(topdir)) {
            addDir(subf, topdir, types, tos);
        } else if (subf.isDirectory()) {
            dirs.add(subf);
        } else if (subf.getName().endsWith(".meta")) {
            file = subf.getPath().substring(0, subf.getPath().indexOf(".meta"));
            f = new File(file);
            if (!f.exists() || f.isDirectory())
                files.add(f);
        } else {
            files.add(subf);
        }
    }
    for (File file : files)
        addFile(file, topdir, types, tos);
    for (File dir : dirs)
        addDir(dir, topdir, types, tos);
}

From source file:org.openmrs.module.report.util.FileUtils.java

public static List<String> listFile(String path) {
    //harsh  28/6/2012 jenkin : Category CORRECTNESS - Type RV_RETURN_VALUE_IGNORED
    path = path.replaceAll("//", "/");
    File file = new File(path);
    List<String> list = null;
    if (file != null && file.isDirectory()) {
        String[] temp = file.list();
        if (temp != null && temp.length > 0) {
            list = new ArrayList<String>();
            for (String t : temp) {
                String newPath = path + t;
                //harsh  28/6/2012 jenkin : Category CORRECTNESS - Type RV_RETURN_VALUE_IGNORED
                newPath = newPath.replaceAll("//", "/");
                File fileTemp = new File(newPath);
                if (fileTemp != null && fileTemp.isFile())
                    list.add(newPath);//from  w  w w .  jav  a 2 s .c o  m
            }
            return list;
        }

    }
    return null;
}

From source file:org.openmrs.module.report.util.FileUtils.java

public static boolean deleteDir(File dir) {
    //      System.out.println(dir);
    if (dir != null && dir.exists() && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            children[i] = dir.getAbsolutePath() + "/" + children[i].toString();
            boolean success = deleteDir(new File(children[i].toString()));
            if (!success) {
                return false;
            }//  w  w  w .ja  v a 2s  .  com
        }
    }
    // The directory is now empty so delete it
    return dir.delete();
}

From source file:net.dataforte.commons.resources.ClassUtils.java

/**
 * Returns all resources beneath a folder. Supports filesystem, JARs and JBoss VFS
 * /*  w  w  w  .  java 2 s .  c  om*/
 * @param folder
 * @return
 * @throws IOException
 */
public static URL[] getResources(String folder) throws IOException {
    List<URL> urls = new ArrayList<URL>();
    ArrayList<File> directories = new ArrayList<File>();
    try {
        ClassLoader cld = Thread.currentThread().getContextClassLoader();
        if (cld == null) {
            throw new IOException("Can't get class loader.");
        }
        // Ask for all resources for the path
        Enumeration<URL> resources = cld.getResources(folder);
        while (resources.hasMoreElements()) {
            URL res = resources.nextElement();
            String resProtocol = res.getProtocol();
            if (resProtocol.equalsIgnoreCase("jar")) {
                JarURLConnection conn = (JarURLConnection) res.openConnection();
                JarFile jar = conn.getJarFile();
                for (JarEntry e : Collections.list(jar.entries())) {
                    if (e.getName().startsWith(folder) && !e.getName().endsWith("/")) {
                        urls.add(new URL(
                                joinUrl(res.toString(), "/" + e.getName().substring(folder.length() + 1)))); // FIXME: fully qualified name
                    }
                }
            } else if (resProtocol.equalsIgnoreCase("vfszip") || resProtocol.equalsIgnoreCase("vfs")) { // JBoss 5+
                try {
                    Object content = res.getContent();
                    Method getChildren = content.getClass().getMethod("getChildren");
                    List<?> files = (List<?>) getChildren.invoke(res.getContent());
                    Method toUrl = null;
                    for (Object o : files) {
                        if (toUrl == null) {
                            toUrl = o.getClass().getMethod("toURL");
                        }
                        urls.add((URL) toUrl.invoke(o));
                    }
                } catch (Exception e) {
                    throw new IOException("Error while scanning " + res.toString(), e);
                }
            } else if (resProtocol.equalsIgnoreCase("file")) {
                directories.add(new File(URLDecoder.decode(res.getPath(), "UTF-8")));
            } else {
                throw new IOException("Unknown protocol for resource: " + res.toString());
            }
        }
    } catch (NullPointerException x) {
        throw new IOException(folder + " does not appear to be a valid folder (Null pointer exception)");
    } catch (UnsupportedEncodingException encex) {
        throw new IOException(folder + " does not appear to be a valid folder (Unsupported encoding)");
    }

    // For every directory identified capture all the .class files
    for (File directory : directories) {
        if (directory.exists()) {
            // Get the list of the files contained in the package
            String[] files = directory.list();
            for (String file : files) {
                urls.add(new URL("file:///" + joinPath(directory.getAbsolutePath(), file)));
            }
        } else {
            throw new IOException(
                    folder + " (" + directory.getPath() + ") does not appear to be a valid folder");
        }
    }
    URL[] urlsA = new URL[urls.size()];
    urls.toArray(urlsA);
    return urlsA;
}