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

public static void synchronize(File source, File destination, boolean smart, long chunkSize)
        throws IOException {
    if (chunkSize <= 0) {
        System.out.println("Chunk size must be positive: using default value.");
        chunkSize = DEFAULT_COPY_BUFFER_SIZE;
    }/*from www  .  j av  a  2s.c  o  m*/
    if (source.isDirectory()) {
        if (!destination.exists()) {
            if (!destination.mkdirs()) {
                throw new IOException("Could not create path " + destination);
            }
        } else if (!destination.isDirectory()) {
            throw new IOException("Source and Destination not of the same type:" + source.getCanonicalPath()
                    + " , " + destination.getCanonicalPath());
        }
        String[] sources = source.list();
        Set<String> srcNames = new HashSet<String>(Arrays.asList(sources));
        String[] dests = destination.list();

        //delete files not present in source
        for (String fileName : dests) {
            if (!srcNames.contains(fileName)) {
                delete(new File(destination, fileName));
            }
        }
        //copy each file from source
        for (String fileName : sources) {
            File srcFile = new File(source, fileName);
            File destFile = new File(destination, fileName);
            synchronize(srcFile, destFile, smart, chunkSize);
        }
    } else {
        if (destination.exists() && destination.isDirectory()) {
            delete(destination);
        }
        if (destination.exists()) {
            long sts = source.lastModified() / FAT_PRECISION;
            long dts = destination.lastModified() / FAT_PRECISION;
            //do not copy if smart and same timestamp and same length
            if (!smart || sts == 0 || sts != dts || source.length() != destination.length()) {
                copyFile(source, destination, chunkSize);
            }
        } else {
            copyFile(source, destination, chunkSize);
        }
    }
}

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

/**
 * Deletes all files in the directory but not the directory itself
 * @param dirNameIn//from   www .j av  a  2 s.  c  o  m
 * @param filePrefixIn Null to delete all files or the prefix of the files to delete
 * @return True if all files were deleted
 */
public static boolean deleteFilesFromDirectory(final String dirNameIn, final String filePrefixIn) {

    boolean fileNotDeleted = false;
    String filePrefix = filePrefixIn;
    if (filePrefix != null) {
        filePrefix = filePrefix.toUpperCase();
    }
    File dir = new File(dirNameIn);

    if (dir.isDirectory() == false) {
        throw new IllegalArgumentException("Specified path [" + dirNameIn + "] is not a directory");
    }

    if (dir.exists()) {
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                String fileName = children[i];
                if (filePrefix == null || fileName.toUpperCase().startsWith(filePrefix)) {
                    File f = new File(dir, children[i]);
                    if (f.isFile()) {
                        if (f.delete() == false) {
                            fileNotDeleted = true;
                        }
                    }
                }
            }
        }
    }
    return (fileNotDeleted == false);

}

From source file:edu.stanford.epadd.launcher.Splash.java

public static boolean deleteDir(File f) {
    if (f.isDirectory()) {
        String[] children = f.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(f, children[i]));
            if (!success) {
                System.out.println("warning: failed to delete file " + f);
                return false;
            }/*from w w  w .  j av a2s . co  m*/
        }
    }

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

From source file:com.google.acre.script.JSFile.java

public static Scriptable jsStaticFunction_files(Context cx, Scriptable thisObj, Object[] args,
        Function funObj) {/*from w  w w  . j  a  va  2 s .  c  o m*/
    String[] files;
    Scriptable scope = ScriptableObject.getTopLevelScope(funObj);

    class ArgLadenFilenameFilter implements FilenameFilter {
        private Object _matcher;
        private Scriptable _scope;
        private Scriptable _this;
        private Context _cx;

        public ArgLadenFilenameFilter(Context cx, Scriptable scope, Scriptable thisObj, Object matcher) {
            _cx = cx;
            _scope = scope;
            _this = thisObj;
            _matcher = matcher;
        }

        public boolean accept(File dir, String fn) {
            if (_matcher instanceof Function) {
                Object[] fnargs = new Object[] { fn };
                return (Boolean) ((Function) _matcher).call(_cx, _scope, _this, fnargs);
            } else if (_matcher instanceof String) {
                return fn.matches((String) _matcher);
            } else {
                return false;
            }
        }
    }

    // This is our own copy of the CostCollector.
    CostCollector __costCollector = CostCollector.getInstance();
    __costCollector.collect("aidc");
    final long start_time = System.currentTimeMillis();

    if (args.length == 1) {
        File dir = new File((String) args[0]);
        files = dir.list();
    } else if (args.length == 2) {
        File dir = new File((String) args[0]);
        FilenameFilter fnfilter = new ArgLadenFilenameFilter(cx, scope, thisObj, args[1]);
        files = dir.list(fnfilter);
    } else {
        return null;
    }
    __costCollector.collect("aidw", System.currentTimeMillis() - start_time);

    if (files == null) {
        return null;
    } else {
        ArrayList<Object> files_list = new ArrayList<Object>();
        for (String f : files) {
            files_list.add(f);
        }

        Scriptable res = cx.newArray(scope, files_list.toArray());
        return res;
    }
}

From source file:moskitt4me.repositoryClient.core.util.RepositoryClientUtil.java

public static void copyDirectory(File sourceLocation, File targetLocation) throws Exception {

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdirs();/*from ww w  .j a va2 s .c  om*/
            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                File source = new File(sourceLocation, children[i]);
                File target = new File(targetLocation, children[i]);
                copyDirectory(source, target);
            }
        }
    } else {
        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?/*from   w  w  w  .j ava  2  s . c  om*/
 *
 * @param src
 *            ??
 * @param des
 *            ?
 * @throws IOException
 */
public static void copy(File srcLocation, File desLocation) throws IOException {
    if (srcLocation.isDirectory()) {
        if (!desLocation.exists() && !desLocation.mkdirs()) {
            throw new IOException("can't create dir " + desLocation.getAbsolutePath());
        }
        String[] children = srcLocation.list();
        for (int i = 0; i < children.length; i++) {
            copy(new File(srcLocation, children[i]), new File(desLocation, children[i]));
        }
    } else {
        File direcory = desLocation.getParentFile();
        if (direcory != null && !direcory.exists() && !direcory.mkdirs()) {
            throw new IOException("can't create dir " + direcory.getAbsolutePath());
        }

        InputStream in = new FileInputStream(srcLocation);
        OutputStream out = new FileOutputStream(desLocation);
        byte[] buf = new byte[XConstant.BUFFER_LEN];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }
}

From source file:com.farmerbb.secondscreen.util.U.java

private static String[] getListOfProfiles(File file) {
    return file.list();
}

From source file:gate.util.Files.java

/**
 * This method takes a regular expression and a directory name and returns
 * the set of Files that match the pattern under that directory.
 *
 * @param regex regular expression path that begins with <code>pathFile</code>
 * @param pathFile directory path where to search for files
 * @return set of file paths under <code>pathFile</code> that matches
 *  <code>regex</code>/*  www. j a va 2  s  . co m*/
 */
public static Set<String> Find(String regex, String pathFile) {
    Set<String> regexfinal = new HashSet<String>();
    String[] tab;
    File file = null;

    //open a file
    try {
        file = new File(pathFile);
    } catch (NullPointerException npe) {
        npe.printStackTrace(Err.getPrintWriter());
    }

    Pattern pattern = Pattern.compile("^" + regex);

    if (file.isDirectory()) {
        tab = file.list();
        for (int i = 0; i <= tab.length - 1; i++) {
            String finalPath = pathFile + "/" + tab[i];
            Matcher matcher = pattern.matcher(finalPath);
            if (matcher.matches()) {
                regexfinal.add(finalPath);
            }
        }
    } else {
        if (file.isFile()) {
            Matcher matcher = pattern.matcher(pathFile);
            if (matcher.matches()) {
                regexfinal.add(pathFile);
            }
        }
    }

    return regexfinal;
}

From source file:co.mcme.animations.animations.commands.AnimationFactory.java

public static void delete(File file) throws IOException {

    if (file.isDirectory()) {
        //directory is empty, then delete it
        if (file.list().length == 0) {
            file.delete();//from   w  ww  . j a va  2  s.c o m
            System.out.println("Directory is deleted : " + file.getAbsolutePath());
        } else {
            //list all the directory contents
            String files[] = file.list();
            for (String temp : files) {
                //construct the file structure
                File fileDelete = new File(file, temp);
                //recursive delete
                delete(fileDelete);
            }

            //check the directory again, if empty then delete it
            if (file.list().length == 0) {
                file.delete();
                System.out.println("Directory is deleted : " + file.getAbsolutePath());
            }
        }
    } else {
        //if file, then delete it
        file.delete();
        System.out.println("File is deleted : " + file.getAbsolutePath());
    }
}

From source file:com.evolveum.midpoint.test.ldap.OpenDJController.java

/**
 * Delete a directory and its contents.//  w ww  .  j  a  v a 2 s.  c o  m
 * 
 * @param dir
 *            The name of the directory to delete.
 * @throws IOException
 *             If the directory could not be deleted.
 */
public static void deleteDirectory(File dir) throws IOException {
    if (dir.isDirectory()) {
        // Recursively delete sub-directories and files.
        for (String child : dir.list()) {
            deleteDirectory(new File(dir, child));
        }
    }

    dir.delete();
}