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:gov.nih.nci.caintegrator.common.Cai2Util.java

/**
 * Takes in a directory and zips it up./*  w  w w . j av a2 s  .c  om*/
 *
 * @param dir - Directory to zip.
 * @return Zipped file, stored in same location, with same name as the directory with .zip attached.
 * @throws IOException - In case cannot be read.
 */
public static File zipAndDeleteDirectory(String dir) throws IOException {
    File directory = new File(dir);
    if (!directory.isDirectory()) {
        throw new IllegalArgumentException("Not a directory:  " + dir);
    }
    int index = directory.getPath().indexOf(directory.getName());
    String[] entries = directory.list();
    if (entries.length == 0) {
        return null;
    }
    File zipfile = new File(dir + ZIP_FILE_SUFFIX);
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
    addDir(directory, out, index);
    out.close();
    FileUtils.deleteDirectory(directory);
    return zipfile;
}

From source file:de.suse.swamp.webswamp.SwampUIManager.java

public static ArrayList getValidSkins() {
    ArrayList skins = new ArrayList();
    String resourcesDirectory = TurbinePull.getResourcesDirectory();
    File uiDir = null;
    try {/*from w  w w . j a  va2s.  c  o m*/
        uiDir = new File(resourcesDirectory);
    } catch (RuntimeException e) {
        Logger.ERROR("wskin path (" + resourcesDirectory + ") not found. " + "Error: " + e.getMessage());
    }
    if (uiDir != null) {
        String[] userSkins = uiDir.list();
        if (userSkins != null) {
            for (int i = 0; i < userSkins.length; i++) {
                skins.add(userSkins[i]);
            }
        }
    }
    return skins;
}

From source file:com.cyberocw.habittodosecretary.file.StorageHelper.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 ww .ja  va 2 s .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 {
        res = copyFile(sourceLocation, targetLocation);
    }
    return res;
}

From source file:com.evolveum.midpoint.util.MiscUtil.java

/**
 * Copy a directory and its contents./*from  w w w  . j  av a2 s  .c  om*/
 *
 * @param src
 *            The name of the directory to copy.
 * @param dst
 *            The name of the destination directory.
 * @throws IOException
 *             If the directory could not be copied.
 */
public static void copyDirectory(File src, File dst) throws IOException {
    if (src.isDirectory()) {
        // Create the destination directory if it does not exist.
        if (!dst.exists()) {
            dst.mkdirs();
        }

        // Recursively copy sub-directories and files.
        for (String child : src.list()) {
            copyDirectory(new File(src, child), new File(dst, child));
        }
    } else {
        MiscUtil.copyFile(src, dst);
    }
}

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

/**
 * Deletes all directories that are empty and are <b>older</b> 
 * than the given date.  If the date is <code>null</code>, all empty 
 * directories will be deleted, regardless of their date.
 * @param parentDir the directory where to start looking for empty 
 *        directories//from   www . jav  a  2s . co m
 * @param date the date to compare empty directories against
 * @return the number of deleted directories
 * @since 1.3.0
 */
public static int deleteEmptyDirs(File parentDir, final Date date) {
    final MutableInt dirCount = new MutableInt(0);
    visitEmptyDirs(parentDir, new IFileVisitor() {
        @Override
        public void visit(File file) {
            if (date == null || FileUtils.isFileOlder(file, date)) {
                String[] children = file.list();
                if (file.isDirectory() && (children == null || children.length == 0)) {
                    try {
                        FileUtil.delete(file);
                        dirCount.increment();
                    } catch (IOException e) {
                        LOG.error("Could not be delete directory: " + file, e);
                    }
                }
            }
        }
    });
    return dirCount.intValue();
}

From source file:forge.limited.BoosterDraft.java

/** Looks for draft files, reads them, returns a list. */
private static List<CustomLimited> loadCustomDrafts() {
    String[] dList;/*from www. j  a  v a 2s. c  om*/
    final List<CustomLimited> customs = new ArrayList<>();

    // get list of custom draft files
    final File dFolder = new File(ForgeConstants.DRAFT_DIR);
    if (!dFolder.exists()) {
        throw new RuntimeException("BoosterDraft : folder not found -- folder is " + dFolder.getAbsolutePath());
    }

    if (!dFolder.isDirectory()) {
        throw new RuntimeException("BoosterDraft : not a folder -- " + dFolder.getAbsolutePath());
    }

    dList = dFolder.list();

    for (final String element : dList) {
        if (element.endsWith(FILE_EXT)) {
            final List<String> dfData = FileUtil.readFile(ForgeConstants.DRAFT_DIR + element);
            customs.add(CustomLimited.parse(dfData, FModel.getDecks().getCubes()));
        }
    }
    return customs;
}

From source file:com.thejustdo.util.Utils.java

/**
 * Creates a ZIP file containing all the files inside a directory.
 * @param location Directory to read./*from   w w w .j  av  a 2s .  co m*/
 * @param pathname Name and path where to store the file.
 * @throws FileNotFoundException If can't find the initial directory.
 * @throws IOException If can't read/write.
 */
public static void zipDirectory(File location, File pathname) throws FileNotFoundException, IOException {

    BufferedInputStream origin;
    FileOutputStream dest = new FileOutputStream(pathname);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    byte data[] = new byte[2048];

    // 1. Get a list of files from current directory
    String files[] = location.list();
    File f;

    // 2. Adding each file to the zip-set.
    for (String s : files) {
        log.info(String.format("Adding: %s", s));
        f = new File(location, s);
        FileInputStream fi = new FileInputStream(f);
        origin = new BufferedInputStream(fi, 2048);
        ZipEntry entry = new ZipEntry(location.getName() + File.separator + s);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, 2048)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();
    }
    out.close();
}

From source file:com.glaf.core.util.ZipUtils.java

private static void recurseFiles(JarOutputStream jos, File file, String s)
        throws IOException, FileNotFoundException {

    if (file.isDirectory()) {
        s = s + file.getName() + "/";
        jos.putNextEntry(new JarEntry(s));
        String as[] = file.list();
        if (as != null) {
            for (int i = 0; i < as.length; i++)
                recurseFiles(jos, new File(file, as[i]), s);
        }//from   ww w.j a v a2s.c  o  m
    } else {
        if (file.getName().endsWith("MANIFEST.MF") || file.getName().endsWith("META-INF/MANIFEST.MF")) {
            return;
        }
        JarEntry jarentry = new JarEntry(s + file.getName());
        FileInputStream fileinputstream = new FileInputStream(file);
        BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);
        jos.putNextEntry(jarentry);
        while ((len = bufferedinputstream.read(buf)) >= 0) {
            jos.write(buf, 0, len);
        }
        bufferedinputstream.close();
        jos.closeEntry();
    }
}

From source file:com.dycody.android.idealnote.utils.StorageHelper.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 . jav  a 2 s. 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");
            res = false;
        }
    }
    return res;
}

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

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