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:eu.sisob.uma.footils.File.FileFootils.java

public static void copyFolder(File src, File dest) throws IOException {

    if (src.isDirectory()) {

        //if directory not exists, create it
        if (!dest.exists()) {
            dest.mkdir();//from  w w w . j  av  a  2  s .co  m
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
            //construct the src and dest file structure
            File srcFile = new File(src, file);
            File destFile = new File(dest, file);
            //recursive copy
            copyFolder(srcFile, destFile);
        }

    } else {
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes 
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
    }
}

From source file:com.app.server.EARDeployer.java

public static boolean deleteDir(File dir) {
    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 w  w .ja  v  a2  s  .co m
        }
    }

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

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

/**
 * Recursively deletes files on directory. Taken from:
 * <strong>http://www.mkyong.com/java/how-to-delete-directory-in-java/</strong>
 *
 * @param file What to clean.//  www.j a v a2 s . c  om
 * @throws IOException If couldn't delete.
 */
public static void delete(File file) throws IOException {

    if (file.isDirectory()) {

        //directory is empty, then delete it
        if (file.list().length == 0) {

            file.delete();
            log.log(Level.INFO, "Directory is deleted : {0}", 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();
                log.log(Level.INFO, "Directory is deleted : {0}", file.getAbsolutePath());
            }
        }

    } else {
        //if file, then delete it
        file.delete();
        log.log(Level.INFO, "File is deleted : {0}", file.getAbsolutePath());
    }
}

From source file:net.rim.ejde.internal.builders.ResourceBuilder.java

private static void cleanupTempSubdir(String name) {
    if (name == null)
        return;/*  w  w  w.  j a v a2  s .c  o m*/
    try {
        File dir = new File(name);
        String[] inDir = dir.list();
        if (inDir != null) {
            for (int i = 0; i < inDir.length; ++i) {
                File subDir = new File(dir, inDir[i]);
                if (subDir.isDirectory()) {
                    removeFiles(subDir);
                } else {
                    subDir.delete();
                }
            }
        }
        dir.delete();
    } catch (IOException ioe) {
        log.error(ioe);
    }
}

From source file:org.shareok.data.documentProcessor.DocumentProcessorUtil.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();/*  w w w .  j  a  v a2 s .  c om*/
            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.elastica.helper.FileUtility.java

public static void copyFile(final File srcPath, final File dstPath) throws IOException {

    if (srcPath.isDirectory()) {
        if (!dstPath.exists()) {
            dstPath.mkdir();/*  www .  j  a v  a 2s. co  m*/
        }

        String[] files = srcPath.list();
        for (String file : files) {
            copyFile(new File(srcPath, file), new File(dstPath, file));
        }
    } else {
        if (!srcPath.exists()) {
            throw new IOException("Directory Not Found ::: " + srcPath);
        } else {
            InputStream in = null;
            OutputStream out = null;
            try {
                if (!dstPath.getParentFile().exists()) {
                    dstPath.getParentFile().mkdirs();
                }

                in = new FileInputStream(srcPath);
                out = new FileOutputStream(dstPath);

                // Transfer bytes
                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java

private static void listFiles(File file, StringBuffer sb) {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method DocumentUtil.listFiles(File file,StringBuffer sb)");
    }//from   ww w  .  j  a v a 2s.  c o m
    String[] list = file.list();
    sb.append("<ul>"); //$NON-NLS-1$
    for (String fileOrFolder : list) {
        File newFile = new File(file.toString() + File.separator + fileOrFolder);
        if (newFile.isHidden())
            continue;
        /*if(newFile.isDirectory()){
        sb.append("<li>"); //$NON-NLS-1$
        sb.append("<a href=./"); //$NON-NLS-1$
        sb.append(newFile.getName());
        sb.append("\">"); //$NON-NLS-1$
        sb.append(newFile.getPath());
        sb.append("</a>"); //$NON-NLS-1$
        sb.append("</li>"); //$NON-NLS-1$
        listFiles(newFile, sb);
        } else {*/
        sb.append("<li>"); //$NON-NLS-1$
        sb.append("<a href=" + "\"" + "./"); //$NON-NLS-1$
        sb.append(newFile.getName());
        sb.append("\">"); //$NON-NLS-1$
        sb.append(newFile.getName());
        sb.append("</a>"); //$NON-NLS-1$
        sb.append("</li>"); //$NON-NLS-1$
        //            }
    }
    sb.append("</ul>"); //$NON-NLS-1$
}

From source file:FileHelper.java

public static boolean areInSync(File source, File destination) throws IOException {
    if (source.isDirectory()) {
        if (!destination.exists()) {
            return false;
        } else if (!destination.isDirectory()) {
            throw new IOException("Source and Destination not of the same type:" + source.getCanonicalPath()
                    + " , " + destination.getCanonicalPath());
        }//from  w ww  .ja  va  2s  . c  o m
        String[] sources = source.list();
        Set<String> srcNames = new HashSet<String>(Arrays.asList(sources));
        String[] dests = destination.list();

        // check for files in destination and not in source
        for (String fileName : dests) {
            if (!srcNames.contains(fileName)) {
                return false;
            }
        }

        boolean inSync = true;
        for (String fileName : sources) {
            File srcFile = new File(source, fileName);
            File destFile = new File(destination, fileName);
            if (!areInSync(srcFile, destFile)) {
                inSync = false;
                break;
            }
        }
        return inSync;
    } else {
        if (destination.exists() && destination.isFile()) {
            long sts = source.lastModified() / FAT_PRECISION;
            long dts = destination.lastModified() / FAT_PRECISION;
            return sts == dts;
        } else {
            return false;
        }
    }
}

From source file:jfix.util.Reflections.java

/**
 * Returns all instanceable (sub-)classes of given type in given package.
 *//* www.j a  v a  2s.  co m*/
public static <E> E[] find(Class<E> classType, Package pckage) {
    File directory;
    try {
        String name = "/" + pckage.getName().replace('.', '/');
        directory = new File(classType.getResource(name).toURI());
    } catch (URISyntaxException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    List<E> result = new ArrayList<>();
    if (directory.exists()) {
        String[] files = directory.list();
        for (int i = 0; i < files.length; i++) {
            if (files[i].endsWith(".class")) {
                String classname = files[i].substring(0, files[i].length() - 6);
                try {
                    Object o = Class.forName(pckage.getName() + "." + classname).newInstance();
                    if (classType.isInstance(o)) {
                        result.add((E) o);
                    }
                } catch (ClassNotFoundException cnfex) {
                    System.err.println(cnfex);
                } catch (InstantiationException iex) {
                } catch (IllegalAccessException iaex) {
                }
            }
        }
    }
    result.sort(new Comparator<Object>() {
        public int compare(Object o1, Object o2) {
            return o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
        }
    });
    return result.toArray((E[]) Array.newInstance(classType, result.size()));
}

From source file:net.duckling.ddl.util.FileUtil.java

public static boolean deleteFilesInFolder(String strFolder) {
    boolean flag = false;
    File folder = new File(strFolder);
    if (!folder.exists()) {
        return flag;
    }/*  w w w. j  a va  2s . c o  m*/
    String[] files = folder.list();
    for (int i = 0; i < files.length; i++) {
        String fileName = strFolder + File.separator + files[i];
        File file = new File(fileName);
        if (file.isDirectory()) {
            flag = deleteFolders(fileName);
        } else {
            file.delete();
        }
    }
    return flag;
}