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.testmax.util.FileUtility.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   ww  w .ja v a  2  s  .c o  m*/
            System.out.println("Directory copied from " + src + "  to " + dest);
        }

        //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();
        System.out.println("File copied from " + src + " to " + dest);
    }
}

From source file:autohit.common.Utils.java

/**
 *  Wipe a directory and everything in it
 * @param dest path to the directory//from  ww w  . ja v  a 2  s  .  co  m
 * @return string containing errors or messages
 */
public static String wipeDir(String dest) {

    String[] directory;
    File fsource = new File(dest);
    StringBuffer messages = new StringBuffer();
    String newdest;
    File newf;

    try {

        // get the source directory list
        directory = fsource.list();

        // rove the directory list
        for (int i = 0; i < directory.length; i++) {
            try {
                newdest = dest + '/' + directory[i];
                newf = new File(newdest);
                if (newf.isDirectory()) {
                    messages.append("Wipe directory=" + newdest + Constants.CRUDE_SEPERATOR);
                    messages.append(Utils.wipeDir(newdest));
                } else {
                    newf.delete();
                }
            } catch (Exception e) {
                messages.append("Error on wipe=" + dest + Constants.CRUDE_SEPERATOR);
            }
        }
        fsource.delete();
        messages.append("Done wipe of directory= " + dest + Constants.CRUDE_SEPERATOR);

    } catch (Exception e) {
        messages.append(
                "Serious error on wipe of= " + dest + " error=" + e.getMessage() + Constants.CRUDE_SEPERATOR);
    }
    return messages.toString();
}

From source file:es.eucm.eadandroid.homeapp.repository.resourceHandler.RepoResourceHandler.java

/**
 * Deletes the specified directory// www .j ava 2 s .c o  m
 */
public static boolean removeDirectory(File directory) {

    if (directory == null)
        return false;
    if (!directory.exists())
        return true;
    if (!directory.isDirectory())
        return false;

    String[] list = directory.list();

    if (list != null) {
        for (int i = 0; i < list.length; i++) {
            File entry = new File(directory, list[i]);

            if (entry.isDirectory()) {
                if (!removeDirectory(entry))
                    return false;
            } else {
                if (!entry.delete())
                    return false;
            }
        }
    }

    return directory.delete();
}

From source file:com.clutch.ClutchSync.java

private static boolean copyDir(File src, File dest) throws IOException {
    if (!src.exists()) {
        Log.e(TAG, "Copy source does not exist: " + src);
        return false;
    }//  www. j a v  a  2  s.  c o  m
    String[] files = src.list();
    if (files == null) {
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (!dest.exists()) {
            dest.createNewFile();
        }
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.close();
        return true;
    }

    for (String file : files) {
        if (!copyDir(new File(src, file), new File(dest, file))) {
            return false;
        }
    }

    return true;
}

From source file:com.diffplug.gradle.FileMisc.java

/** Deletes all empty folders (recursively). */
public static void deleteEmptyFolders(File d) throws IOException {
    retry(d, root -> {/*  w  w  w  .j  a  v a  2 s.co m*/
        // define the directory hierarchy
        TreeDef<File> dirTree = file -> Arrays.stream(file.listFiles()).filter(File::isDirectory)
                .collect(Collectors.toList());
        // find all the empty directories
        List<File> emptyDirs = TreeStream.depthFirst(dirTree, root).filter(dir -> dir.list().length == 0)
                .collect(Collectors.toList());
        for (File emptyDir : emptyDirs) {
            File toDelete = emptyDir;
            while (!toDelete.equals(root)) {
                Preconditions.checkArgument(toDelete.delete(), "Failed to delete %s", toDelete);
                toDelete = toDelete.getParentFile();
                if (toDelete.list().length > 0) {
                    break;
                }
            }
        }
        return null;
    });
}

From source file:de.fraunhofer.iosb.ivct.IVCTcommander.java

protected static Map<String, List<String>> readTestSuiteFiles(final String testsuite) {
    Map<String, List<String>> xyz = new HashMap<String, List<String>>();
    File mine;
    int i;//  www .  j av a2 s .  c  o  m
    String path = configParameters.pathTestsuite + "\\" + testsuite;
    String files[];
    mine = new File(path);
    files = mine.list();
    if (files == null) {
        return null;
    }
    for (i = 0; i < files.length; i++) {
        String p = new String(path + "\\" + files[i]);
        mine = new File(p);
        if (mine.isFile()) {
            List<String> ls;
            ls = readFile(p);
            xyz.put(files[i], ls);
        }
    }
    return xyz;
}

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

/**
 * Visits all files (and only files) under a directory, including 
 * sub-directories.//  ww w. ja va  2s  .co m
 * @param dir the directory
 * @param visitor the visitor
 */
public static void visitAllFiles(File dir, IFileVisitor visitor) {
    if (!dir.exists()) {
        return;
    } else if (dir.isDirectory()) {
        String[] children = dir.list();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                visitAllFiles(new File(dir, children[i]), visitor);
            }
        }
    } else {
        visitor.visit(dir);
    }
}

From source file:autohit.common.Utils.java

/**
 *  Copy entire directories.  I can't believe I have to write this.
 * @param source source is the source directory, without a training slash
 * @param dest destination is the destination directory, without a training slash
 * @param wipe destructive copy.  If true, it will destroy destination directories before
 * copying the files.  The files themselves are always overwritten.
 * @return string containing errors or messages
 *///from   w w w  .java  2s  . c o  m
public static String copyDir(String source, String dest, boolean wipe) {

    String[] directory;
    String destinationfile = "";
    String sourcefile;
    File fsource = new File(source);
    File fdest = new File(dest);
    int size;
    StringBuffer messages = new StringBuffer();

    try {

        // get the source directory list
        directory = fsource.list();

        // create the destination directory.  If it already exists, wipe it.
        if (fdest.exists()) {
            if (wipe == true) {
                messages.append(Utils.wipeDir(dest));
                fdest.mkdirs();
                messages.append("Make directory=" + dest + Constants.CRUDE_SEPERATOR);
            }
        } else {
            fdest.mkdirs();
            messages.append("Make directory=" + dest + Constants.CRUDE_SEPERATOR);
        }

        // rove the directory list
        for (int i = 0; i < directory.length; i++) {
            try {
                destinationfile = dest + '/' + directory[i];
                sourcefile = source + '/' + directory[i];
                fsource = new File(sourcefile);
                if (fsource.isDirectory()) {
                    messages.append("Is directory=" + sourcefile + Constants.CRUDE_SEPERATOR);
                    messages.append(Utils.copyDir(sourcefile, destinationfile, wipe));
                } else {
                    messages.append(Utils.copy(sourcefile, destinationfile));
                }
            } catch (Exception e) {
                messages.append("Error on=" + destinationfile + Constants.CRUDE_SEPERATOR);
            }
        }
        messages.append("Done with directory= " + source + Constants.CRUDE_SEPERATOR);

    } catch (Exception e) {
        messages.append("Serious error on=" + dest + " error=" + e.getMessage() + Constants.CRUDE_SEPERATOR);
    }
    return messages.toString();
}

From source file:org.openmrs.module.webservices.docs.ResourceDocCreator.java

/**
 * Fills a map of resource names and their documentation objects with resource operations.
 * /*from   w w w. ja v  a 2 s.c  o  m*/
 * @param resouceDocMap a map of each resource name and its corresponding documentation object.
 */
private static void fillOperations(Map<String, ResourceDoc> resouceDocMap) throws IOException {

    File directory = new File(
            new File("").getAbsolutePath() + "/src/main/java/org/openmrs/module/webservices/rest/web/resource");

    //Look for all resource files in the resource folder.
    String[] files = directory.list();
    if (files == null)
        return;

    for (int i = 0; i < files.length; i++) {
        String file = files[i];

        //We are only interested in ......Resource.java files
        if (file.endsWith("Resource.java")) {

            //Resource name is class name without the .java extension
            String name = file.subSequence(0, file.length() - "Resource.java".length()).toString();
            ResourceDoc resourceDoc = resouceDocMap.get(name);

            //Get the complete path and name of the java source file.
            String fullPathName = directory.getAbsolutePath() + File.separator + file;

            String source = OpenmrsUtil.getFileAsString(new File(fullPathName));

            //Parse the file's JavaDoc annotations to get the supported web service operations.
            resourceDoc.setOperations(JavadocParser.parse(source));
        }
    }
}

From source file:io.joynr.util.JoynrUtil.java

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

    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            if (targetLocation.mkdir() == false) {
                logger.debug("Creating target directory " + targetLocation + " failed.");
            }//from ww w .  j  a v  a2 s.c  om
        }

        String[] children = sourceLocation.list();
        if (children == null) {
            return;
        }

        for (int i = 0; i < children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
        }
    } else {
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(sourceLocation);
            out = new FileOutputStream(targetLocation);
            copyStream(in, out);
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}