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.farmerbb.secondscreen.util.U.java

private static String[] getListOfProfiles(File file, String fakeEntry) {
    String[] list = file.list();
    String[] realList = new String[list.length + 1];

    System.arraycopy(list, 0, realList, 0, list.length);

    realList[list.length] = fakeEntry;//from  w ww . j av  a 2  s.  c  om

    return realList;
}

From source file:MainClass.java

public Object[][] getFileStats(File dir) {
    String files[] = dir.list();
    Object[][] results = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
        File tmp = new File(files[i]);
        results[i][0] = new Boolean(tmp.isDirectory());
        results[i][1] = tmp.getName();/*ww w  .j ava2s .  c o  m*/
        results[i][2] = new Boolean(tmp.canRead());
        results[i][3] = new Boolean(tmp.canWrite());
        results[i][4] = new Long(tmp.length());
        results[i][5] = new Date(tmp.lastModified());
    }
    return results;
}

From source file:MainClass.java

public void setFileStats(File dir) {
    String files[] = dir.list();
    data = new Object[files.length][titles.length];

    for (int i = 0; i < files.length; i++) {
        File tmp = new File(files[i]);
        data[i][0] = new Boolean(tmp.isDirectory());
        data[i][1] = tmp.getName();/*  w  w  w. j  a  v a2s  .  c om*/
        data[i][2] = new Boolean(tmp.canRead());
        data[i][3] = new Boolean(tmp.canWrite());
        data[i][4] = new Long(tmp.length());
        data[i][5] = new Date(tmp.lastModified());
    }

    fireTableDataChanged();
}

From source file:emperior.Main.java

public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {
    System.out.println(sourceLocation.getName() + " - " + targetLocation.getName());
    if (sourceLocation.isDirectory()) {
        if (FileTree.isFolderAccepted(sourceLocation.getName())) {
            if (!targetLocation.exists()) {
                targetLocation.mkdirs();
            }/*  w  w w  .  ja v a  2 s .  c om*/

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
            }
        }

    } else {

        InputStream in = new FileInputStream(sourceLocation);
        OutputStream out = new FileOutputStream(targetLocation);

        // Copy the bits from instream to outstream
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();

        Main.addLineToLogFile("[File] saving to: " + targetLocation);
    }
}

From source file:com.roche.sequencing.bioinformatics.common.utils.FileUtil.java

public static boolean isEmptyDirectory(File workflowDirectory) {
    boolean isDirectory = workflowDirectory.isDirectory();
    String[] files = workflowDirectory.list();
    boolean isEmptyDirectory = isDirectory && (files == null || (files != null && files.length == 0));
    return isEmptyDirectory;
}

From source file:io.siddhi.doc.gen.core.utils.DocumentationUtils.java

/**
 * Deploy the mkdocs website on GitHub pages
 *
 * @param version       The version of the documentation
 * @param baseDirectory The base directory of the project
 * @param url           The SCM URL/*w ww. j a v a 2  s . c  o  m*/
 * @param scmUsername   The SCM username
 * @param scmPassword   The SCM password
 * @param logger        The maven logger
 */
public static void deployMkdocsOnGitHubPages(String version, File baseDirectory, String url, String scmUsername,
        String scmPassword, Log logger) {
    try {
        // Find initial branch name
        List<String> gitStatusOutput = getCommandOutput(
                new String[] { Constants.GIT_COMMAND, Constants.GIT_BRANCH_COMMAND }, logger);
        String initialBranch = null;
        for (String gitStatusOutputLine : gitStatusOutput) {
            if (gitStatusOutputLine.startsWith(Constants.GIT_BRANCH_COMMAND_OUTPUT_CURRENT_BRANCH_PREFIX)) {
                initialBranch = gitStatusOutputLine
                        .substring(Constants.GIT_BRANCH_COMMAND_OUTPUT_CURRENT_BRANCH_PREFIX.length());
            }
        }

        if (initialBranch != null) {
            // Stash changes
            executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_STASH_COMMAND }, logger);

            // Change to gh-pages branch. This will not do anything if a new branch was created in the last command.
            executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_CHECKOUT_COMMAND,
                    Constants.GIT_GH_PAGES_BRANCH }, logger);

            // Create branch if it does not exist. This will fail if the branch exists and will not do anything.
            executeCommand(
                    new String[] { Constants.GIT_COMMAND, Constants.GIT_CHECKOUT_COMMAND,
                            Constants.GIT_CHECKOUT_COMMAND_ORPHAN_ARGUMENT, Constants.GIT_GH_PAGES_BRANCH },
                    logger);

            executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_PULL_COMMAND,
                    Constants.GIT_REMOTE, Constants.GIT_GH_PAGES_BRANCH }, logger);

            // Getting the site that was built by mkdocs
            File siteDirectory = new File(Constants.MKDOCS_SITE_DIRECTORY);
            FileUtils.copyDirectory(siteDirectory, baseDirectory);
            String[] siteDirectoryContent = siteDirectory.list();

            // Pushing the site to GitHub (Assumes that site/ directory is ignored by git)
            if (siteDirectoryContent != null && siteDirectoryContent.length > 0) {
                List<String> gitAddCommand = new ArrayList<>();
                Collections.addAll(gitAddCommand, Constants.GIT_COMMAND, Constants.GIT_ADD_COMMAND);
                Collections.addAll(gitAddCommand, siteDirectoryContent);
                executeCommand(gitAddCommand.toArray(new String[gitAddCommand.size()]), logger);

                List<String> gitCommitCommand = new ArrayList<>();
                Collections.addAll(gitCommitCommand, Constants.GIT_COMMAND, Constants.GIT_COMMIT_COMMAND,
                        Constants.GIT_COMMIT_COMMAND_MESSAGE_ARGUMENT,
                        String.format(Constants.GIT_COMMIT_COMMAND_MESSAGE_FORMAT, version, version),
                        Constants.GIT_COMMIT_COMMAND_FILES_ARGUMENT);
                Collections.addAll(gitCommitCommand, siteDirectoryContent);
                executeCommand(gitCommitCommand.toArray(new String[gitCommitCommand.size()]), logger);

                if (scmUsername != null && scmPassword != null && url != null) {
                    // Using scm username and password env var values
                    String urlWithUsernameAndPassword = url.replaceFirst(Constants.GITHUB_URL,
                            Constants.GITHUB_URL_WITH_USERNAME_PASSWORD);
                    executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_PUSH_COMMAND,
                            String.format(urlWithUsernameAndPassword, scmUsername, scmPassword),
                            Constants.GIT_GH_PAGES_BRANCH }, logger);

                } else {
                    // Using git credential store
                    executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_PUSH_COMMAND,
                            Constants.GIT_REMOTE, Constants.GIT_GH_PAGES_BRANCH }, logger);
                }
            }

            // Changing back to initial branch
            executeCommand(
                    new String[] { Constants.GIT_COMMAND, Constants.GIT_CHECKOUT_COMMAND, initialBranch },
                    logger);
            executeCommand(new String[] { Constants.GIT_COMMAND, Constants.GIT_STASH_COMMAND,
                    Constants.GIT_STASH_POP_COMMAND }, logger);
        } else {
            logger.warn("Unable to parse git-status command and retrieve current git branch. "
                    + "Skipping deployment.");
        }
    } catch (Throwable t) {
        logger.warn("Failed to deploy the documentation on github pages.", t);
    }
}

From source file:io.stallion.utils.ResourceHelpers.java

public static List<String> listFilesInDirectory(String plugin, String path) {
    String ending = "";
    String starting = "";
    if (path.contains("*")) {
        String[] parts = StringUtils.split(path, "*", 2);
        String base = parts[0];/*from   w  w w  .  j av a 2s .  com*/
        if (!base.endsWith("/")) {
            path = new File(base).getParent();
            starting = FilenameUtils.getName(base);
        } else {
            path = base;
        }
        ending = parts[1];
    }

    Log.info("listFilesInDirectory Parsed Path {0} starting:{1} ending:{2}", path, starting, ending);
    URL url = PluginRegistry.instance().getJavaPluginByName().get(plugin).getClass().getResource(path);
    Log.info("URL: {0}", url);

    List<String> filenames = new ArrayList<>();
    URL dirURL = getClassForPlugin(plugin).getResource(path);
    Log.info("Dir URL is {0}", dirURL);
    // Handle file based resource folder
    if (dirURL != null && dirURL.getProtocol().equals("file")) {
        String fullPath = dirURL.toString().substring(5);
        File dir = new File(fullPath);
        // In devMode, use the source resource folder, rather than the compiled version
        if (Settings.instance().getDevMode()) {
            String devPath = fullPath.replace("/target/classes/", "/src/main/resources/");
            File devFolder = new File(devPath);
            if (devFolder.exists()) {
                dir = devFolder;
            }
        }
        Log.info("List files from folder {0}", dir.getAbsolutePath());
        List<String> files = list();
        for (String name : dir.list()) {
            if (!empty(ending) && !name.endsWith(ending)) {
                continue;
            }
            if (!empty(starting) && !name.endsWith("starting")) {
                continue;
            }
            // Skip special files, hidden files
            if (name.startsWith(".") || name.startsWith("~") || name.startsWith("#")
                    || name.contains("_flymake.")) {
                continue;
            }
            filenames.add(path + name);
        }
        return filenames;
    }

    if (dirURL.getProtocol().equals("jar")) {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
        JarFile jar = null;
        try {
            jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (path.startsWith("/")) {
            path = path.substring(1);
        }
        Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
        Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
        while (entries.hasMoreElements()) {
            String name = entries.nextElement().getName();
            Log.finer("Jar file entry: {0}", name);
            if (name.startsWith(path)) { //filter according to the path
                String entry = name.substring(path.length());
                int checkSubdir = entry.indexOf("/");
                if (checkSubdir >= 0) {
                    // if it is a subdirectory, we just return the directory name
                    entry = entry.substring(0, checkSubdir);
                }
                if (!empty(ending) && !name.endsWith(ending)) {
                    continue;
                }
                if (!empty(starting) && !name.endsWith("starting")) {
                    continue;
                }
                // Skip special files, hidden files
                if (name.startsWith(".") || name.startsWith("~") || name.startsWith("#")
                        || name.contains("_flymake.")) {
                    continue;
                }
                result.add(entry);
            }
        }
        return new ArrayList<>(result);
    }
    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
    /*
    try {
    URL url1 = getClassForPlugin(plugin).getResource(path);
    Log.info("URL1 {0}", url1);
    if (url1 != null) {
        Log.info("From class folder contents {0}", IOUtils.toString(url1));
        Log.info("From class folder contents as stream {0}", IOUtils.toString(getClassForPlugin(plugin).getResourceAsStream(path)));
    }
    URL url2 = getClassLoaderForPlugin(plugin).getResource(path);
    Log.info("URL1 {0}", url2);
    if (url2 != null) {
        Log.info("From classLoader folder contents {0}", IOUtils.toString(url2));
        Log.info("From classLoader folder contents as stream {0}", IOUtils.toString(getClassLoaderForPlugin(plugin).getResourceAsStream(path)));
    }
            
    } catch (IOException e) {
    Log.exception(e, "error loading path " + path);
    }
    //  Handle jar based resource folder
    try(
        InputStream in = getResourceAsStream(plugin, path);
        BufferedReader br = new BufferedReader( new InputStreamReader( in ) ) ) {
    String resource;
    while( (resource = br.readLine()) != null ) {
        Log.finer("checking resource for inclusion in directory scan: {0}", resource);
        if (!empty(ending) && !resource.endsWith(ending)) {
            continue;
        }
        if (!empty(starting) && !resource.endsWith("starting")) {
            continue;
        }
        // Skip special files, hidden files
        if (resource.startsWith(".") || resource.startsWith("~") || resource.startsWith("#") || resource.contains("_flymake.")) {
            continue;
        }
        Log.finer("added resource during directory scan: {0}", resource);
        filenames.add(path + resource);
    }
    } catch (IOException e) {
    throw new RuntimeException(e);
    }
    return filenames;
    */
}

From source file:io.treefarm.plugins.haxe.utils.HaxelibHelper.java

public static int injectPomHaxelib(String artifactId, String artifactVersion, String artifactType,
        File artifactFile, Logger logger) {
    //File unpackDirectory = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);
    File unpackDir = getLibUnpackPath(artifactId, artifactVersion);

    if (!unpackDir.exists() || artifactFile.lastModified() > unpackDir.lastModified()) {
        File libDir = getHaxelibDirectoryForArtifactAndInitialize(artifactId, artifactVersion, logger);

        if (unpackDir.exists()) {
            FileUtils.deleteQuietly(unpackDir);
        }/* www .  java2 s .  c om*/
        if (libDir.exists()) {
            FileUtils.deleteQuietly(libDir);
        }

        UnpackHelper unpackHelper = new UnpackHelper() {
        };
        DefaultUnpackMethods unpackMethods = new DefaultUnpackMethods(logger);
        try {
            unpackHelper.unpack(unpackDir, artifactType, artifactFile, unpackMethods, null);
        } catch (Exception e) {
            logger.error(String.format("Can't unpack %s: %s", artifactId, e));
        }

        for (String fileName : unpackDir.list()) {
            if (artifactType.equals("jar")) {
                fileName = getUniqueLibPath(artifactId, artifactVersion);
            }
            File firstFile = new File(unpackDir, fileName);
            firstFile.renameTo(libDir);
            break;
        }

        unpackDir.setLastModified(artifactFile.lastModified());
    }

    int returnValue = 0;

    // set to specified version
    returnValue = setVersionFor(artifactId, artifactVersion, logger);

    return returnValue;
}

From source file:FileTableDemo.java

public FileTableModel(File dir) {
    this.dir = dir;
    this.filenames = dir.list(); // Store a list of files in the directory
}

From source file:br.edimarmanica.trinity.check.CheckNrPages.java

private int nrPagesBase() {
    File dir = new File(Paths.PATH_BASE + site.getPath());
    return dir.list().length;
}