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:gdt.data.entity.facet.ExtensionHandler.java

public static String getClasspath(Entigrator entigrator) {
    try {// ww  w .  j a v  a  2s.  co  m
        String[] sa = entigrator.indx_listEntities("entity", "extension");
        if (sa == null)
            return null;
        Sack extension;
        //=entigrator.getEntityAtKey(extension$);
        String lib$;
        //=extension.getElementItemAt("field", "lib");
        String jar$;
        //="jar:file:" +entigrator.getEntihome()+"/"+extension$+"/"+lib$+"!/";
        File file;
        StringBuffer sb = new StringBuffer();
        String[] ca;
        String separator$ = System.getProperty("path.separator");
        if (sa != null) {
            for (String s : sa) {
                try {
                    extension = entigrator.getEntityAtKey(s);
                    lib$ = extension.getElementItemAt("field", "lib");
                    file = new File(entigrator.getEntihome() + "/" + s + "/" + lib$);
                    if (file.exists())
                        sb = sb.append(separator$ + file.getPath());
                    ca = extension.elementListNoSorted("classpath");
                    if (ca != null) {

                        for (String c : ca) {
                            file = new File(entigrator.getEntihome() + "/" + s + "/" + s);
                            if (file.exists())
                                sb = sb.append(separator$ + file.getPath());
                        }
                    }
                } catch (Exception ee) {
                    Logger.getLogger(ExtensionHandler.class.getName()).info(ee.toString());
                }
            }
        }
        File dependencies = new File(entigrator.getEntihome() + "/" + Entigrator.DEPENDENCIES);
        if (dependencies.exists() && dependencies.isDirectory()) {
            String[] da = dependencies.list();
            if (da != null)
                for (String d : da) {
                    if (d.endsWith(".jar"))
                        sb = sb.append(separator$ + entigrator.getEntihome() + "/" + Entigrator.DEPENDENCIES
                                + "/" + d);
                }

        }
        return sb.toString();
        /*
         String path$ = JMainConsole.class.getProtectionDomain().getCodeSource().getLocation().getPath();
           // System.out.println("Community:main.path="+path$);
            String jar$ = URLDecoder.decode(path$, "UTF-8");
            jar$=jar$.replace("file:", "");
            jar$=jar$.replace("!/", "");
            File jar=new File(jar$);
            */
    } catch (Exception e) {
        Logger.getLogger(ExtensionHandler.class.getName()).severe(e.toString());
    }
    return null;
}

From source file:javadocofflinesearch.tools.Commandline.java

private static void verifySingle(File ca) {
    if (ca.exists()) {
        if (ca.list().length > 0) {
            System.out.println("looks OK: " + ca.getAbsolutePath() + " items: " + toStringList(ca.list()));
        } else {/*from  w w  w.  j av  a  2s .c  o m*/
            System.out.println("Warning!: " + ca.getAbsolutePath() + " is empty!");
        }
    } else {
        System.out.println("Error!: " + ca.getAbsolutePath() + " dont exists!");
    }
}

From source file:com.bristle.javalib.io.FileUtil.java

/**************************************************************************
* Returns a sorted array of Strings naming the files and directories in
* the specified directory./*from  w ww. ja  v a  2  s .  c om*/
*@param  directory  The directory to search
*@return            Sorted array of file and directories in the directory.
**************************************************************************/
public static String[] getSortedNames(File directory) {
    String[] arrNames = directory.list();
    if (arrNames != null) {
        Arrays.sort(arrNames, String.CASE_INSENSITIVE_ORDER);
    }
    return arrNames;
}

From source file:org.apache.servicemix.platform.testing.support.SmxPlatform.java

/**
 * Delete the given file (can be a simple file or a folder).
 *
 * @param file the file to be deleted//from ww  w.ja  va  2  s .c o  m
 * @return if the deletion succeded or not
 */
public static boolean delete(File file) {

    // bail out quickly
    if (file == null)
        return false;

    // recursively delete children file
    boolean success = true;

    if (file.isDirectory()) {
        String[] children = file.list();
        for (int i = 0; i < children.length; i++) {
            success &= delete(new File(file, children[i]));
        }
    }

    // The directory is now empty so delete it
    return (success &= file.delete());
}

From source file:it.reexon.lib.files.FileUtils.java

/**
 * Utility method for copying directory 
 *  /*  w  w w.j a v a 2 s.c o m*/
 * @param srcDir - source directory 
 * @param dstDir - destination directory 
 * 
 * @throws IllegalArgumentException if srcDir or dstDir are null or is not exists or srcDir is not a directory
 * @throws IOException  If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.Author:
 */
public static void copyDirectory(File srcDir, File dstDir) throws IOException {
    if (srcDir == null || dstDir == null)
        throw new IllegalArgumentException("SrcDir is null");
    if (!srcDir.exists())
        throw new IllegalArgumentException("SrcDir is not exists");

    if (srcDir.isDirectory()) {
        if (!dstDir.exists()) {
            dstDir.mkdir();
        }

        for (String aChildren : srcDir.list()) {
            copyDirectory(new File(srcDir, aChildren), new File(dstDir, aChildren));
        }
    } else {
        copyFile(srcDir, dstDir);
    }
}

From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java

private static void delete(File file) throws IOException {
    if (file.isDirectory()) {
        if (file.list().length == 0) {
            file.delete();//from w ww  . j  av a  2 s.c o m
        } else {
            String files[] = file.list();
            for (String temp : files) {
                File fileDelete = new File(file, temp);
                delete(fileDelete);
            }

            //check the directory again, if empty then delete it
            if (file.list().length == 0) {
                file.delete();
            }
        }

    } else {
        //if file, then delete it
        file.delete();
    }
}

From source file:core.AnnotationTask.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();/*w w  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:org.xmlactions.common.io.ResourceUtils.java

/**
 * Deletes all files and subdirectories under dir.
 * /*w  ww.j  a v a2  s .  c  o  m*/
 * @return true if all deletions were successful. If a deletion fails, the
 *         method stops attempting to delete and returns false.
 */
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;
            }
        }
    }
    // The directory is now empty so delete it
    return dir.delete();
}

From source file:DirectoryWalker.java

/**
 * Get all the Directories in the starting directory (and sub dirs)
 * returns only files ending this the filename
 * @param startingDirectory// www . java 2  s.c  o m
 * @return filesFound (as HashSet)
 * @exception java.io.IOException
 */
public static HashSet getDirs(String startingDirectory) throws java.io.IOException {

    //Local Variables
    String tmpFullFile;
    String tmpSubDir;
    File startDir = new File(startingDirectory);
    File tmpFile;
    String[] thisDirContents;
    HashSet dirsFound = new HashSet();
    HashSet subDirFilesFound;

    //Check that this is a valid directory
    if (!startDir.isDirectory()) {
        throw new java.io.IOException(startingDirectory + " was not a valid directory");
    }

    //Add the current directory to the output list
    dirsFound.add(startingDirectory);

    //Get the contents of the current directory
    thisDirContents = startDir.list();

    if (thisDirContents != null) {
        //Now loop through , apply filter , or adding them to list of sub dirs
        for (int a = 0; a < thisDirContents.length; a++) {

            //Get Handle to (full) file (inc path)  
            tmpFullFile = FileUtil.combineFileAndDirectory(thisDirContents[a], startingDirectory);

            tmpFile = new File(tmpFullFile);

            //We're only interested in directories
            if (tmpFile.isDirectory()) {

                //Add this to the directory list
                dirsFound.add(tmpFullFile);

                //Now Do Recursive Call (to this method)if Directory
                tmpSubDir = FileUtil.combineFileAndDirectory(thisDirContents[a], startingDirectory);
                subDirFilesFound = DirectoryWalker.getDirs(tmpSubDir);
                dirsFound.addAll(subDirFilesFound);
            }

        }
    }

    return dirsFound;

}

From source file:fr.iphc.grid.jobmonitor.CeList.java

static public Boolean initDirectory(File directory) {
    if (directory == null)
        return false;
    if (!directory.exists())
        return directory.mkdir();
    if (!directory.isDirectory())
        return false;

    String[] list = directory.list();

    // Some JVMs return null for File.list() when the
    // directory is empty.
    if (list != null) {
        for (int i = 0; i < list.length; i++) {
            File entry = new File(directory, list[i]);
            if (entry.isDirectory()) {
                if (!initDirectory(entry))
                    return false;
            } else {
                if (!entry.delete())
                    return false;
            }/*  w w  w .j a  va  2 s  .  c  o  m*/
        }
    }
    return true;
}