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:com.application.error.ExceptionHandler.java

/**
 * Search for stack trace files.//from   w  w w . j a v  a  2s  .c om
 * @return
 */
private static String[] searchForStackTraces() {
    if (stackTraceFileList != null) {
        return stackTraceFileList;
    }
    File dir = new File(G.FILES_PATH + "/");
    // Try to create the files folder if it doesn't exist
    dir.mkdir();
    // Filter for ".stacktrace" files
    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".stacktrace");
        }
    };
    return (stackTraceFileList = dir.list(filter));
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * //from  ww w  .ja va  2 s.  co  m
 * @param directory
 * @param extension
 * @return A list of fileNames in the directory with the specified extension.
 */
public static String[] getAllFileNamesWithExtension(File directory, final String extension) {
    String[] ret = directory.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(extension);
        }
    });
    return ret == null ? new String[0] : ret;
}

From source file:com.github.hexocraft.worldrestorer.WorldRestorerApi.java

public static ArrayList<String> listWorlds(JavaPlugin plugin) {
    File worldSavesFolder = new File(plugin.getDataFolder() + "/" + worldSaves + "/");
    String[] directories = worldSavesFolder.list(new FilenameFilter() {
        @Override/*  w ww . jav a 2  s.  c o  m*/
        public boolean accept(File file, String name) {
            if (file.isDirectory()) {
                String[] settings = file.list(new FilenameFilter() {
                    @Override
                    public boolean accept(File file, String name) {
                        return name.equals(worldSettings);
                    }
                });
                if (settings != null)
                    return true;
                return false;
            } else
                return false;
        }
    });

    if (directories == null)
        return Lists.newArrayList();
    else
        return Lists.newArrayList(directories);
}

From source file:gov.nasa.ensemble.common.io.FileUtilities.java

/**
 * //w w  w  . ja  v  a  2 s.com
 * @param directory
 * @param extensions
 *            the list of acceptable extensions
 * @return A list of fileNames in the directory with the specified extension.
 */
public static String[] getAllFileNamesWithExtension(File directory, final String... extensions) {
    String[] ret = directory.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            boolean ret = false;
            for (String extension : extensions)
                ret |= name.endsWith(extension);
            return ret;
        }
    });
    return ret == null ? new String[0] : ret;
}

From source file:com.quinsoft.zeidon.utils.JoeUtils.java

/**
 * Returns java.io.File for a file matching path using CASE-INSENSITIVE file
 * matching.  This makes it easier for us to run on Windows.
 *
 * @param path/*from  ww  w. j  a v a2  s .  co  m*/
 * @return
 */
public static File getFile(String path) {
    try {
        File file = new File(path);
        if (file.exists())
            return file;

        // Try searching for the file without regard to case.
        String parentPath = file.getParent();
        if (StringUtils.isBlank(parentPath))
            return file; // No parent path so just return.

        File dir = new File(parentPath);
        NameFileFilter filter = new NameFileFilter(file.getName(), IOCase.INSENSITIVE);
        String[] files = dir.list(filter);
        if (files == null)
            return file; // We probably didn't find a directory so just return the unknown file.

        if (files.length == 1)
            return new File(parentPath + File.separator + files[0]);

        if (files.length > 1)
            throw new ZeidonException("Found multiple matching entries for %s", path);

        // We didn't find an exact match so just return.
        return file;
    } catch (Exception e) {
        throw ZeidonException.wrapException(e).prependFilename(path);
    }
}

From source file:levelBuilder.DialogMaker.java

/**
 * Saves graph to file./*from   w  w w. java 2 s .  c om*/
 */
private static void saveGraph() {
    //Checks for errors
    ArrayList<String> errorList = new ArrayList<String>();
    errorList = dg.check();
    if (errorList.isEmpty()) {
        //Save popup.
        String filePath = null;
        File savesFolder = new File(saveDir);
        if (!savesFolder.exists())
            savesFolder.mkdir();
        String[] saveNames = new String[savesFolder.list(fileFilter).length + 1];
        for (int i = 0; i < saveNames.length - 1; i++) {
            saveNames[i] = savesFolder.list(fileFilter)[i];
        }
        saveNames[saveNames.length - 1] = "New File";

        if (saveNames.length > 0) {
            saveName = (String) JOptionPane.showInputDialog(null, "Choose file to overwrite, or a new file",
                    "Choose!", JOptionPane.PLAIN_MESSAGE, null, saveNames, saveName);
            if (saveName == "New File") {
                filePath = JOptionPane.showInputDialog(null, "New file name", "Write!",
                        JOptionPane.PLAIN_MESSAGE);
                filePath = saveDir + filePath + ".xml";
            } else if (saveName == null) {
                return;
            } else {
                filePath = saveDir + saveName;
            }

            //            //Saves position of each node.
            //            for (DialogNode n : nodeMap.values()) {
            //               n.setX(((AbstractLayout<DialogNode, Double>) layout).getX(n));
            //               n.setY(((AbstractLayout<DialogNode, Double>) layout).getY(n));
            //            }   

            //Saves graph
            dg.save(filePath);
        }
    }
    //Display errors
    else {
        JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
        for (String error : errorList)
            panel.add(new JLabel(error));
        JOptionPane.showMessageDialog(null, panel, "Errors. Fix these to continue.", JOptionPane.PLAIN_MESSAGE);
    }
}

From source file:levelBuilder.DialogMaker.java

/**
 * Popup for selecting file to load./*from w  w w . jav  a2 s.c  om*/
 */
private static void loadFilePopup() {
    File savesFolder = new File(saveDir);
    File[] saves = savesFolder.listFiles(fileFilter);
    if (savesFolder.exists()) {
        if (saves.length > 0) {
            String[] saveNames = savesFolder.list(fileFilter);
            saveName = (String) JOptionPane.showInputDialog(null, "Which file would you like to load?",
                    "Choose!", JOptionPane.PLAIN_MESSAGE, null, saveNames, saveNames[0]);
            if (saveName == null) {
                splashWindow();
            } else {
                loadGraph(false);
            }
        } else {
            JOptionPane.showMessageDialog(null, "No existing save files", null, JOptionPane.PLAIN_MESSAGE);
            splashWindow();
        }
    } else {
        savesFolder.mkdir();
        JOptionPane.showMessageDialog(null, "No existing save files", null, JOptionPane.PLAIN_MESSAGE);
        splashWindow();
    }

}

From source file:marytts.util.io.FileUtils.java

/**
 * List the basenames of all files in directory that end in suffix, without that suffix.
 * For example, if suffix is ".wav", return the names of all .wav files in the
 * directory, but without the .wav extension. The file names
 * are sorted in alphabetical order, according to java's string search.
 * @param directory// w w w.j av  a2 s. c o  m
 * @param suffix
 * @return
 */
public static String[] listBasenames(File directory, String suffix) {
    final String theSuffix = suffix;
    String[] filenames = directory.list(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            return name.endsWith(theSuffix);
        }
    });

    /* Sort the file names alphabetically */
    Arrays.sort(filenames);

    for (int i = 0; i < filenames.length; i++) {
        filenames[i] = filenames[i].substring(0, filenames[i].length() - suffix.length());
    }
    return filenames;
}

From source file:com.momock.util.Logger.java

public static void open(Context context, final String name, int maxLogFiles, int level) {
    if (!enabled)
        return;//  w ww.j  av a 2  s  .  co  m
    appName = context.getPackageName();
    logName = name;
    logFileName = logName + "[" + new SimpleDateFormat("yyyyMMddHHmmss", Locale.US).format(new Date())
            + "].log";
    if (logStream == System.out) {
        File logDir = null;
        try {
            if (getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                logDir = Environment.getExternalStorageDirectory();
            } else if (context != null) {
                logDir = context.getCacheDir();
            }
            if (logDir != null) {
                android.util.Log.d("Logger", logDir.getAbsolutePath());
                String[] fs = logDir.list(new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String filename) {
                        return filename.startsWith(logName + "[") && filename.endsWith("].log");
                    }

                });
                List<String> allLogs = new ArrayList<String>();
                for (int i = 0; fs != null && i < fs.length; i++)
                    allLogs.add(fs[i]);
                Collections.sort(allLogs);
                for (int i = 0; i < allLogs.size() - maxLogFiles + 1; i++)
                    new File(logDir, allLogs.get(i)).delete();
                logStream = new PrintStream(new FileOutputStream(new File(logDir, logFileName), false));
            }
        } catch (IOException e) {
            android.util.Log.e("Logger", "Fails to create log file!", e);
        }
    }
    logLevel = level;

    logStream.println("========== Logger Begin ==========");
    logStream.flush();
}

From source file:de.andreasschoknecht.LS3.DocumentCollection.java

/**
 * Filters the PNML files in a directory, which possibly contains other files as well.
 *
 * @param dir The directory containing PNML files and possibly other files
 * @return the PNML files in the directory
 *///from   www.  j a va2  s.co  m
private static String[] filterPNMLFiles(File dir) {
    // Check if directory can be accessed
    if (!dir.isDirectory()) {
        System.out.println("Error: could not open directory " + dir.getAbsolutePath());
        System.exit(-2);
    }
    // Filter PNML files
    String[] fileList = dir.list(new FilenameFilter() {
        public boolean accept(File d, String name) {
            return name.endsWith(".pnml");
        }
    });
    return fileList;
}