List of utility methods to do Folder Read
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()]); |
Collection | getFilesRegex(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);
|
HashSet | getFilesStartingWith(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); }); ... |
String | getFileStatus(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(); |
String | getFileString(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(); |
String | getFileString(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(); ... |
String | getFileStringContent(String filename) get File String Content String text = new Scanner(new File(filename)).useDelimiter("\\A").next(); return text; |
ArrayList | GetFilesWithChildren(File root, ArrayList 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()); ... |
Properties | getFileSystemProperties(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; |