Java Utililty Methods Folder Read

List of utility methods to do Folder Read

Description

The list of methods to do Folder Read are organized into topic(s).

Method

File[]getFilesOfTypeInDirectory(File directory, String filetype)
Returns an array of files of the specified type in the specified directory.
List<File> fileList = new ArrayList<File>();
for (String filename : directory.list()) {
    if (filename.toLowerCase().endsWith("." + filetype.trim().toLowerCase())) {
        fileList.add(new File(directory.getAbsolutePath() + File.separator + filename));
return fileList.toArray(new File[fileList.size()]);
CollectiongetFilesRegex(final File root, final String[] regex)
Returns recursively all files in a directory that have a path whose suffix beyond root is matched by regex.
return getFilesRegex(root.getAbsolutePath(), root, regex);
HashSetgetFilesStartingWith(File parentDir, String prefix)
get Files Starting With
String[] list = parentDir.list();
HashSet<String> hashSet = new HashSet<String>();
if (list != null) {
    for (String string : list) {
        if (string.startsWith(prefix)) {
            hashSet.add(string);
return hashSet;
File[]getFilesStartingWith(String dirName, String startsWith)
get Files Starting With
File dir = new File(dirName);
startChars = startsWith;
return dir.listFiles(new FilenameFilter() {
    @Override
    public boolean accept(File dir, String fileName) {
        return fileName.startsWith(startChars);
});
...
StringgetFileStatus(File file)
get File Status
StringBuffer buffer = new StringBuffer();
buffer.append(file.isHidden() ? FILE_STATUS_HIDDEN + "\t" : "\t");
buffer.append(file.canRead() ? FILE_STATUS_CAN_READ + "\t" : "\t");
buffer.append(file.canWrite() ? FILE_STATUS_CAN_WRITE + "\t" : "\t");
return buffer.toString();
StringgetFileString(File file)
get File String
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try {
    reader = new BufferedReader(new FileReader(file));
    String text = null;
    while ((text = reader.readLine()) != null) {
        contents.append(text).append(System.getProperty("line.separator"));
} catch (FileNotFoundException e) {
    throw new RuntimeException(e);
} catch (IOException e) {
    throw new RuntimeException(e);
} finally {
    try {
        if (reader != null) {
            reader.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
return contents.toString();
StringgetFileString(String filePath)
get File String
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringBuilder sb = new StringBuilder();
String r = null;
while ((r = br.readLine()) != null) {
    sb.append(r);
return sb.toString();
...
StringgetFileStringContent(String filename)
get File String Content
String text = new Scanner(new File(filename)).useDelimiter("\\A").next();
return text;
ArrayListGetFilesWithChildren(File root, ArrayList files)
Gets all files with child directory
File[] list = root.listFiles();
if (list == null)
    return files;
for (File p : list) {
    if (p.isDirectory()) {
        GetFilesWithChildren(p, files);
    } else {
        files.add(p.getAbsolutePath());
...
PropertiesgetFileSystemProperties(String path)
get File System Properties
Properties props = null;
try {
    InputStream is = new BufferedInputStream(new FileInputStream(new File(path)));
    props = new Properties();
    props.load(is);
} catch (IOException e) {
    e.printStackTrace();
return props;