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(FilenameFilter filter) 

Source Link

Document

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

Usage

From source file:Main.java

public static void traverseFolder(Map<String, File> fileMap, File rootFolder, File folderInCheck) {
    if (folderInCheck.isFile()) {
        int length = (int) rootFolder.getAbsolutePath().length();
        String pathKey = folderInCheck.getAbsolutePath().substring(length);
        fileMap.put(pathKey, folderInCheck);
        return;/*from  w  w w. ja v  a 2 s  . c om*/
    }

    String[] names = folderInCheck.list(new FilenameFilter() {
        // for filtering some files, such as ".xml"
        public boolean accept(File dir, String name) {
            return true;
        }
    });

    if (names == null || names.length == 0) {
        return;
    }

    File[] files = folderInCheck.listFiles();
    if (files == null || files.length == 0) {
        return;
    }

    for (File child : files) {
        traverseFolder(fileMap, rootFolder, child);
    }
}

From source file:au.org.ala.delta.util.FileUtils.java

public static File findFileIgnoreCase(File file) {

    if (file.exists()) {
        return file;
    }//  www  .  j  a  v  a  2s.c o m

    File parent = file.getParentFile();

    if (parent == null) {
        parent = new File(System.getProperty("user.dir"));
    }

    if (parent.exists()) {
        String[] matches = parent.list(new CaseInsenstiveFilenameMatcher(file.getName()));
        if (matches.length > 0) {
            String newFilename = String.format("%s%s%s", parent.getAbsolutePath(), File.separator, matches[0]);
            File candidateFile = new File(newFilename);
            if (candidateFile.exists()) {
                return candidateFile;
            }
        }
    }

    return null;
}

From source file:objective.taskboard.it.TemplateIT.java

@AfterClass
public static void cleanupTemplates() {
    File tempDir = FileUtils.getTempDirectory();
    final Pattern tempFilePattern = Pattern
            .compile("(sheet-template.*\\.xml|shared-strings.*\\.xml|Followup.*\\.xlsm)");
    String[] tempFiles = tempDir.list((dir, name) -> tempFilePattern.matcher(name).matches());
    for (String tempFile : tempFiles) {
        FileUtils.deleteQuietly(new File(tempDir, tempFile));
    }/*from w  w  w.jav a  2  s.  co  m*/
}

From source file:org.metasyntactic.utilities.ExceptionUtilities.java

private static String[] searchForStackTraces() {
    File dir = new File(FilesPath + "/");
    dir.mkdirs();/*w w w  .j av  a2 s  . co m*/

    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".stacktrace");
        }
    };
    return dir.list(filter);
}

From source file:TrainLogistic.java

static void mainToOutput(String[] args, PrintWriter output) throws Exception {
    if (parseArgs(args)) {
        double logPEstimate = 0;
        int samples = 0;
        /*read files in dir of inputFile*/
        int fi = 0;//file ID
        File file = new File(inputFile);
        String[] fns = file.list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                if (name.endsWith(".svm")) {
                    return true;
                } else {
                    return false;
                }//w  ww  .  ja va2 s .c  om
            }
        });

        String[] ss = new String[lmp.getNumFeatures() + 1];
        String[] iv = new String[2];
        OnlineLogisticRegression lr = lmp.createRegression();
        while (fi < fns.length) {
            for (int pass = 0; pass < passes; pass++) {
                BufferedReader in = open(inputFile + fns[fi]);
                System.out.println(pass + 1);
                try {
                    // read variable names

                    String line = in.readLine();
                    int lineCount = 1;
                    while (line != null) {
                        // for each new line, get target and predictors
                        Vector input = new RandomAccessSparseVector(lmp.getNumFeatures());
                        ss = line.split(" ");
                        int targetValue;
                        if (ss[0].startsWith("+"))
                            targetValue = 1;
                        else
                            targetValue = 0;
                        int k = 1;
                        while (k < ss.length) {
                            iv = ss[k].split(":");
                            input.setQuick(Integer.valueOf(iv[0]) - 1, Double.valueOf(iv[1]));
                            //System.out.printf("%d-----%d:%.4f====%d\n", k,Integer.valueOf(iv[0])-1,Double.valueOf(iv[1]),lineCount);
                            k++;
                        }
                        input.setQuick(lmp.getNumFeatures() - 1, 1);
                        // check performance while this is still news
                        double logP = lr.logLikelihood(targetValue, input);
                        if (!Double.isInfinite(logP)) {
                            if (samples < 20) {
                                logPEstimate = (samples * logPEstimate + logP) / (samples + 1);
                            } else {
                                logPEstimate = 0.95 * logPEstimate + 0.05 * logP;
                            }
                            samples++;
                        }
                        double p = lr.classifyScalar(input);
                        if (scores) {
                            output.printf(Locale.ENGLISH, "%10d %2d %10.2f %2.4f %10.4f %10.4f\n", samples,
                                    targetValue, lr.currentLearningRate(), p, logP, logPEstimate);
                        }
                        // now update model
                        lr.train(targetValue, input);
                        if ((lineCount) % 1000 == 0)
                            System.out.printf("%d\t", lineCount);
                        line = in.readLine();
                        lineCount++;
                    }
                } finally {
                    Closeables.closeQuietly(in);
                }
                System.out.println();
            }
            fi++;
        }

        FileOutputStream modelOutput = new FileOutputStream(outputFile);
        try {
            saveTo(modelOutput, lr);
        } finally {
            Closeables.closeQuietly(modelOutput);
        }
        /*
              output.printf(Locale.ENGLISH, "%d\n", lmp.getNumFeatures());
              output.printf(Locale.ENGLISH, "%s ~ ", lmp.getTargetVariable());
              String sep = "";
              for (String v : csv.getTraceDictionary().keySet()) {
                double weight = predictorWeight(lr, 0, csv, v);
                if (weight != 0) {
                  output.printf(Locale.ENGLISH, "%s%.3f*%s", sep, weight, v);
                  sep = " + ";
                }
              }
              output.printf("\n");
              model = lr;
              for (int row = 0; row < lr.getBeta().numRows(); row++) {
                for (String key : csv.getTraceDictionary().keySet()) {
                  double weight = predictorWeight(lr, row, csv, key);
                  if (weight != 0) {
                    output.printf(Locale.ENGLISH, "%20s %.5f\n", key, weight);
                  }
                }
                for (int column = 0; column < lr.getBeta().numCols(); column++) {
                  output.printf(Locale.ENGLISH, "%15.9f ", lr.getBeta().get(row, column));
                }
                output.println();
              }*/
    }
}

From source file:com.streamsets.datacollector.util.SystemProcessImpl.java

static void clean(File tempDir, int limit) {
    String[] files = tempDir.list(new FilenameFilter() {
        @Override/*  w  ww . j  ava2 s  .  c  om*/
        public boolean accept(File dir, String name) {
            return name.endsWith(OUT_EXT) || name.endsWith(ERR_EXT);
        }
    });
    if (files != null && files.length > limit) {
        List<String> fileList = new ArrayList<>(files.length);
        fileList.addAll(Arrays.asList(files));
        Collections.sort(fileList);
        while (fileList.size() > limit) {
            File file = new File(tempDir, fileList.remove(0));
            if (!FileUtils.deleteQuietly(file)) {
                LOG.warn("Could not delete: {}", file);
            }
        }
    }
}

From source file:com.chaordicsystems.sstableconverter.SSTableConverter.java

public static Collection<SSTableReader> readSSTables(final IPartitioner srcPartitioner, File srcDir,
        final CFMetaData metadata, final OutputHandler outputHandler) {
    final List<SSTableReader> sstables = new ArrayList<SSTableReader>();
    srcDir.list(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            if (new File(dir, name).isDirectory())
                return false;
            Pair<Descriptor, Component> p = SSTable.tryComponentFromFilename(dir, name);
            Descriptor desc = p == null ? null : p.left;
            if (p == null || !p.right.equals(Component.DATA) || desc.temporary)
                return false;

            if (!new File(desc.filenameFor(Component.PRIMARY_INDEX)).exists()) {
                outputHandler.output(String.format("Skipping file %s because index is missing", name));
                return false;
            }/*from  w w  w  .  jav a  2s. c om*/

            Set<Component> components = new HashSet<Component>();
            components.add(Component.DATA);
            components.add(Component.PRIMARY_INDEX);
            if (new File(desc.filenameFor(Component.COMPRESSION_INFO)).exists())
                components.add(Component.COMPRESSION_INFO);
            if (new File(desc.filenameFor(Component.STATS)).exists())
                components.add(Component.STATS);

            try {
                sstables.add(SSTableReader.openForBatch(desc, components, srcPartitioner, metadata));
            } catch (IOException e) {
                outputHandler
                        .output(String.format("Skipping file %s, error opening it: %s", name, e.getMessage()));
            }
            return false;
        }
    });
    return sstables;
}

From source file:com.hybris.mobile.logging.ExceptionHandler.java

private static String[] searchForStackTraces() {
    if (stackTraceFileList != null) {
        return stackTraceFileList;
    }// w  w  w  .  ja v a  2 s .c  o m
    File dir = new File(RA.FILES_PATH + "/");
    dir.mkdir();
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return name.endsWith(".stacktrace");
        }
    };
    return (stackTraceFileList = dir.list(filter));
}

From source file:ClassPath.java

/**
 * Checks for class path components in the following properties:
 * "java.class.path", "sun.boot.class.path", "java.ext.dirs"
 * /*ww w .ja  v  a2s.  c om*/
 * @return class path as used by default by BCEL
 */
public static final String getClassPath() {
    String class_path = System.getProperty("java.class.path");
    String boot_path = System.getProperty("sun.boot.class.path");
    String ext_path = System.getProperty("java.ext.dirs");
    List list = new ArrayList();
    getPathComponents(class_path, list);
    getPathComponents(boot_path, list);
    List dirs = new ArrayList();
    getPathComponents(ext_path, dirs);
    for (Iterator e = dirs.iterator(); e.hasNext();) {
        File ext_dir = new File((String) e.next());
        String[] extensions = ext_dir.list(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                name = name.toLowerCase(Locale.ENGLISH);
                return name.endsWith(".zip") || name.endsWith(".jar");
            }
        });
        if (extensions != null) {
            for (int i = 0; i < extensions.length; i++) {
                list.add(ext_dir.getPath() + File.separatorChar + extensions[i]);
            }
        }
    }
    StringBuffer buf = new StringBuffer();
    for (Iterator e = list.iterator(); e.hasNext();) {
        buf.append((String) e.next());
        if (e.hasNext()) {
            buf.append(File.pathSeparatorChar);
        }
    }
    return buf.toString().intern();
}

From source file:ClassFinder.java

private static String[] addJarsInPath(String[] paths) {
    Set<String> fullList = new HashSet<String>();
    for (final String path : paths) {
        fullList.add(path); // Keep the unexpanded path
        // TODO - allow directories to end with .jar by removing this check?
        if (!path.endsWith(DOT_JAR)) {
            File dir = new File(path);
            if (dir.exists() && dir.isDirectory()) {
                String[] jars = dir.list(new FilenameFilter() {
                    public boolean accept(File f, String name) {
                        return name.endsWith(DOT_JAR);
                    }/*w w w  .j  a  v  a  2s .  c om*/
                });
                fullList.addAll(Arrays.asList(jars));
            }
        }
    }
    return fullList.toArray(new String[0]);
}