Here you can find the source of getFileNameStartingWith(String directory, String prefix)
Parameter | Description |
---|---|
directory | the full path directory to check |
prefix | the prefix to find |
public static String[] getFileNameStartingWith(String directory, String prefix)
//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]); } }