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.twinsoft.convertigo.engine.util.ZipUtils.java

/*** Added by julienda - 10/09/2012
 * Return the archive name corresponding of the project name
 * @param supposedProject: the (supposed) project
 * @return the path of the real archive name
 * @throws IOException *///from   w w  w  .  j  av  a  2 s.c  om
public static String getArchiveName(String supposedProject) throws IOException {

    //Get the project directory path
    String pathProjects = new File(supposedProject).getParent();
    //Get the supposed project name without ".car"
    String supposedProjectName = new File(supposedProject).getName();
    supposedProjectName = supposedProjectName.substring(0, supposedProjectName.length() - 4);

    File projectPath = new File(pathProjects);
    String realArchiveFile = null;

    Engine.logEngine.trace("ZipUtils.getArchiveName() - projectPath: " + projectPath);

    int i = 0;

    //Create a list with the (path) files of the project directory 
    String[] listfiles = projectPath.list();

    while ((i < listfiles.length)) {

        //If the file is an archive
        if (listfiles[i].endsWith(".car") == true) {
            Engine.logEngine.trace("ZipUtils.getArchiveName() - listfiles[" + i + "]: " + listfiles[i]);
            Engine.logEngine.trace("ZipUtils.getArchiveName() - supposedProjectName: " + supposedProjectName);
            Engine.logEngine.trace(
                    "ZipUtils.getArchiveName() - listfiles PATH: " + new File(projectPath, listfiles[i]));

            //If the project name is equals to the (supposed) project name
            if (getProjectName(new File(projectPath, listfiles[i]).getPath()).equals(supposedProjectName)) {
                return new File(projectPath, listfiles[i]).getName();
            }
            Engine.logEngine.trace("ZipUtils.getArchiveName() - realArchiveFile: " + realArchiveFile);
        }
        i++;
    }

    throw new FileNotFoundException("Supposed project " + supposedProject + " not found!");
}

From source file:com.silverpeas.util.FileUtil.java

public static boolean deleteEmptyDir(File directory) {
    if (directory.exists() && directory.isDirectory() && directory.list() != null
            && directory.list().length == 0) {
        return directory.delete();
    }/*  w w  w  .  j  a  va 2s .  co  m*/
    return false;
}

From source file:com.ryan.ryanreader.cache.CacheManager.java

private static void pruneTemp(final File dir) {

    for (final String file : dir.list()) {

        if (file.endsWith(tempExt)) {
            new File(dir, file).delete();
        }//from w  w w .  ja va 2  s .c  om
    }
}

From source file:com.plugin.excel.xsd.node.store.impl.FileHelper.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 ww.ja  va  2s  .  c  om*/
            // log.info("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();
        // log.info("File copied from " + src + " to " + dest);
    }
}

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

public static void copyFolder(String oldPath, String newPath) {

    try {//from  w  ww .  j  ava2  s.  c o  m
        (new File(newPath)).mkdirs();
        File a = new File(oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        LOG.error(e);
    }

}

From source file:com.tinspx.util.io.ChannelSourceTest.java

static File setupTestDir() throws IOException {
    File testDir = new File(new File(System.getProperty("user.dir")), "test");
    if (testDir.exists()) {
        if (testDir.list().length > 0) {
            throw new IOException(testDir + " exists and is not empty");
        }/*  w w w  .  j  av a 2 s .c o m*/
    } else if (!testDir.mkdirs()) {
        throw new IOException("could not create " + testDir);
    }
    boolean complete = false;
    try {
        Resources.asByteSource(ChannelSource.class.getResource("/" + FORMATTED))
                .copyTo(Files.asByteSink(new File(testDir, FORMATTED)));
        Resources.asByteSource(ChannelSource.class.getResource("/" + SAMPLE))
                .copyTo(Files.asByteSink(new File(testDir, SAMPLE)));
        complete = true;
        return testDir;
    } finally {
        if (!complete) {
            teardownTestDir(testDir).close();
        }
    }
}

From source file:gov.nih.nci.caintegrator.common.Cai2Util.java

/**
 * Lists the directory contents.//from  ww  w .j a  v a2s.c  o m
 * @param dir the File directory of which the contents will be logged.
 */
public static void printDirContents(File dir) {
    String[] files = dir.list();
    if (files != null) {
        LOGGER.info("Listing: Contents of " + files.length + " files in dir " + dir.getAbsolutePath());
        for (int i = 0; i < files.length; i++) {
            LOGGER.info("Listing: Files: " + files[i]);
        }
    } else {
        LOGGER.info("Listing: files object is null.");
    }
}

From source file:com.amalto.core.jobox.util.JoboxUtil.java

private static void deleteAllFiles(String path) {
    File file = new File(path);
    if (!file.exists()) {
        return;/*w w w. j a  v  a  2 s.c o  m*/
    }
    if (!file.isDirectory()) {
        LOGGER.warn("Expected a folder '" + path + "' but was a file."); //$NON-NLS-1$ //$NON-NLS-2$
    }
    String[] tempList = file.list();
    File temp;
    for (String currentTempFile : tempList) {
        if (path.endsWith(File.separator)) {
            temp = new File(path + currentTempFile);
        } else {
            temp = new File(path + File.separator + currentTempFile);
        }
        if (temp.isFile()) {
            if (!temp.delete()) {
                LOGGER.error("Delete folder failed for '" + currentTempFile + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }
        }
        if (temp.isDirectory()) {
            deleteAllFiles(path + "/" + currentTempFile);//$NON-NLS-1$
            deleteFolder(path + "/" + currentTempFile);//$NON-NLS-1$
        }
    }
}

From source file:ai.susi.json.JsonRepository.java

private static File getCurrentDump(File path, String prefix, final Mode mode, final boolean dailyDump) {
    String currentDatePart = dateSuffix(dailyDump, new Date());

    // if there is already a dump, use it
    String[] existingDumps = path.list();
    if (existingDumps != null) {
        for (String d : existingDumps) {
            // first check if the file is the current file: we never compress that to enable a write to the end of the file
            if (d.startsWith(prefix + currentDatePart) && d.endsWith(".txt")) {
                continue;
            }/* w  ww  . ja v a 2s.c o  m*/

            // according to the write mode, we either compress or uncompress the file on-the-fly
            if (mode == COMPRESSED_MODE) {
                // all files should be compressed to enable small file sizes, but contents must be in RAM after reading
                if (d.startsWith(prefix) && d.endsWith(".txt")) {
                    final File source = new File(path, d);
                    final File dest = new File(path, d + ".gz");
                    if (dest.exists())
                        dest.delete();
                    new Thread() {
                        public void run() {
                            try {
                                DAO.log("starting gzip of " + source);
                                Compression.gzip(source, dest, true);
                                DAO.log("finished gzip of " + source);
                            } catch (IOException e) {
                                DAO.log("gzip of " + source + " failed: " + e.getMessage());
                            }
                        }
                    }.start();
                }
            } else {
                // all files should be uncompressed to enable random-access mode
                if (d.startsWith(prefix) && d.endsWith(".gz")) {
                    final File source = new File(path, d);
                    final File dest = new File(path, d.substring(0, d.length() - 3));
                    if (dest.exists())
                        dest.delete();
                    try {
                        Compression.gunzip(source, dest, true);
                    } catch (IOException e) {
                        Log.getLog().warn(e);
                        // mark the file as invalid
                        if (dest.exists())
                            dest.delete();
                        final File invalid = new File(path, d + ".invalid");
                        source.renameTo(invalid);
                    }
                }
            }
        }
        // the latest file with the current date is the required one (and it should not be compressed)
        for (String d : existingDumps) {
            if (d.startsWith(prefix + currentDatePart) && d.endsWith(".txt")) {
                return new File(path, d);
            }
        }
    }
    // no current file was found: create a new one, use a random number.
    // The random is used to make it possible to join many different dumps from different instances
    // without renaming them
    String random = (Long.toString(Math.abs(new Random(System.currentTimeMillis()).nextLong())) + "00000000")
            .substring(0, 8);
    return new File(path, prefix + currentDatePart + "_" + random + ".txt");
}

From source file:org.lol.reddit.cache.CacheManager.java

private static void pruneTemp(final File dir) {

    final String[] list = dir.list();
    if (list == null)
        return;//from   ww  w .  j a  va 2 s .co m

    for (final String file : list) {

        if (file.endsWith(tempExt)) {
            new File(dir, file).delete();
        }
    }
}