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:Main.java

/**
 * Checks if this directory has a file with suffix suffix
 * @param f - directory//  w  w w  .  ja  v a2 s  . c o m
 * @return
 */
public static boolean hasFileWithSuffix(File f, String suffix) {
    if (f.isDirectory()) {
        for (String s : f.list()) {
            if (s.endsWith(suffix))
                return true;
        }
        return false;
    } else {
        return false;
    }
}

From source file:Main.java

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }//from  ww  w. j ava  2s .co  m
        }
    }
    return dir.delete();
}

From source file:Main.java

/**
 * Zip the subdirectory and exclude already zipped files
 * @param path//  w w  w  .  j a va  2 s .  c om
 * @param srcFolder
 * @param zip
 * @param includeFullPath
 * @throws java.io.IOException
 */
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
        throws IOException {
    File folder = new File(srcFolder);

    for (String fileName : folder.list()) {
        if (path.equals("") && !fileName.endsWith(".zip")) {
            if (includeFullPath) {
                addFileToZip(folder.toString(), srcFolder + "/" + fileName, zip);
            } else {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip);
            }
        } else if (!fileName.endsWith(".zip")) {
            addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
        }
    }
}

From source file:Main.java

static boolean isFileOrEmptyDirectory(String path) {
    File f = new File(path);
    if (!f.isDirectory())
        return true;

    String[] list = f.list();
    return ((list == null) || (list.length == 0));
}

From source file:Main.java

private static void innerListFiles(File dir, Collection<File> accumulator, FileFilter filter) {

    String[] filenames = dir.list();

    if (filenames != null) {
        for (int i = 0; i < filenames.length; i++) {
            File file = new File(dir, filenames[i]);
            if (file.isDirectory()) {
                innerListFiles(file, accumulator, filter);
            } else {
                if (filter != null && filter.accept(file)) {
                    accumulator.add(file);
                }/*  ww  w .  j  a  v a2  s .c om*/
            }
        }
    }
}

From source file:Main.java

public static boolean deleteDir(File dir) {
    if (dir != null) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }//w ww .  ja  v  a2s .  c o m
            }
        }
        return dir.delete();
    }
    return true;
}

From source file:Main.java

public static void copyFolder(String oldPath, String newPath) {

    try {/*from w  ww  .ja  v a2s . c om*/
        (new File(newPath)).mkdirs();
        File a = new File(oldPath);
        String[] file = a.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 + "/" + (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[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "copyFolder() copy dir error", e);
    }

}

From source file:Main.java

static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
        throws IOException {
    File folder = new File(srcFolder);
    String listOfFiles[] = folder.list();
    for (int i = 0; i < listOfFiles.length; i++) {
        String folderPath = null;
        if (path.length() < 1) {
            folderPath = folder.getName();
        } else {/*from   ww w  .java 2  s .c o  m*/
            folderPath = path + File.separator + folder.getName();
        }
        String srcFile = srcFolder + File.separator + listOfFiles[i];
        addToZip(folderPath, srcFile, zip);
    }
}

From source file:com.cloudera.sqoop.testutil.DirUtil.java

/**
 * recursively delete a dir and its children.
 * @param dir//from   ww w . j  av  a 2  s  .com
 * @return true on succesful removal of a dir
 */
public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            File f = new File(dir, children[i]);
            boolean success = deleteDir(f);
            if (!success) {
                LOG.warn("Could not delete " + f.getAbsolutePath());
                return false;
            }
        }
    }

    // The directory is now empty so delete it too.
    LOG.debug("Removing: " + dir);
    return dir.delete();
}

From source file:DirectoryTree.java

public static void tree(String filename) {
    File file = new File(filename);

    if (!file.isDirectory()) {
        System.out.println(filename);
        return;//from w  w  w .  j  av  a 2 s  .c  o  m
    }

    String files[] = file.list();
    for (int i = 0; i < files.length; i++) {
        tree(filename + File.separator + files[i]);
    }
}