/*
********************************************************
* GetFilesAsArray
********************************************************
*/
String[] FileUtils.GetFilesAsArray(String directory)
{
File dir = new File(directory);
return dir.list();
}
/*
********************************************************
* ConvertArrayToList
********************************************************
*/
List CollectionUtils.ConvertArrayToList(Object []inArray)
{
ArrayList list = new ArrrayList(inArray.Length);
for(int i=0;i<inArray.Length;i++)
{
list.add(inArray[i]);
}
return list;
}
/*
********************************************************
* CopyArrayToList
********************************************************
*/
List CollectionUtils.CopyArrayToList(Object []inArray, List targetList)
{
for(int i=0;i<inArray.Length;i++)
{
targetList.add(inArray[i]);
}
return targetList;
}
/*
********************************************************
* GetFiles
********************************************************
*/
List FileUtils.GetFiles(String directory)
{
File dir = new File(directory);
return ConvertArrayToList(dir.list());
}
satya - 9/6/2020, 12:14:12 PM
Here is a better implementation
public static List<String> getFiles(String rootPath)
{
File rootDir = new File(rootPath);
Utils.massert("getFiles",
rootDir.exists(),
rootPath + " doesn't exist");
String[] filelist = rootDir.list();
return Arrays.asList(filelist);
}
satya - 9/6/2020, 4:46:13 PM
In 2020 the file list
public static List<File> getFiles(String rootPath)
{
File rootDir = new File(rootPath);
Utils.massert("getFiles",
rootDir.exists(),
rootPath + " doesn't exist");
File[] filelist = rootDir.listFiles();
return Arrays.asList(filelist);
}
public static List<File> getFiles(String rootPath, String wildCardString)
{
File rootDir = new File(rootPath);
Utils.massert("getFiles",
rootDir.exists(),
rootPath + " doesn't exist");
FileFilter wildCardFilter = new WildcardFileFilter(wildCardString);
File[] fileList = rootDir.listFiles(wildCardFilter);
return Arrays.asList(fileList);
}
satya - 9/6/2020, 4:47:47 PM
This uses Apache Commons IO library
This uses Apache Commons IO library
satya - 9/6/2020, 4:48:11 PM
Imports
import java.io.File;
import java.io.FileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
satya - 9/6/2020, 4:49:26 PM
A number of links to java api are here