Java File Name Get getFileNameStartingWith(String directory, String prefix)

Here you can find the source of getFileNameStartingWith(String directory, String prefix)

Description

get an array of all files in the given directory, with given prefix

License

Open Source License

Parameter

Parameter Description
directory the full path directory to check
prefix the prefix to find

Return

an array of all file names in that directory (without full path)

Declaration

public static String[] getFileNameStartingWith(String directory, String prefix) 

Method Source Code

//package com.java2s;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import java.util.Vector;

public class Main {
    /**/*w ww.  j a va  2 s .  c  om*/
     * get an array of all files in the given directory, with given prefix
     * 
     * @param directory
     *            the full path directory to check
     * @param prefix
     *            the prefix to find
     * @return an array of all file names in that directory (without full path)
     */
    public static String[] getFileNameStartingWith(String directory, String prefix) {
        Vector<String> vector = new Vector<String>();
        File f = new File(directory);
        File[] files = f.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].getName().startsWith(prefix)) {
                vector.add(files[i].getName());
            }
        }
        String[] toReturn = new String[vector.size()];
        vector.toArray(toReturn);
        return toReturn;
    }

    public static String[] listFiles(String fileName) {
        return listFiles(new File(fileName));
    }

    public static String[] listFiles(File file) {
        List<String> files = new ArrayList<String>();

        File[] fileArray = file.listFiles();
        if (fileArray != null) {
            for (int i = 0; i < fileArray.length; i++) {
                if (fileArray[i].isFile()) {
                    files.add(fileArray[i].getName());
                }
            }
        }

        return (String[]) files.toArray(new String[0]);
    }
}

Related

  1. getFileNames_STR(String path)
  2. getFileNamesFromDir(File rootDir, String indexName)
  3. getFileNamesInJar(File source, String folder, String extension)
  4. getFileNamesOld(File zipFile)
  5. getFileNamesRecursive(final String pDataDir, final FilenameFilter pFilter, final int pMaxFiles)
  6. getFilenameSuffix(final File file)
  7. getFileNameUtil(String dataPath)
  8. getFileNameWithAddedExtension(File parent, File f, String ext)
  9. getFileNameWithNewExtension(File parent, File file, String ext)