List of usage examples for org.apache.commons.io FileUtils listFiles
public static Collection listFiles(File directory, String[] extensions, boolean recursive)
From source file:com.r573.enfili.common.io.file.FileHelper.java
public static void copyAllFilesInFolder(File srcFolder, File destFolder) { try {//from www.jav a 2s .c o m if (!srcFolder.exists()) { // TODO: ignore for now. Figure out the appropriate error // handling for this later return; } Collection<File> files = FileUtils.listFiles(srcFolder, null, true); for (File file : files) { File destFile = new PathBuilder(destFolder.getPath()).append(file.getName()).toFile(); FileUtils.copyFile(file, destFile); } } catch (IOException e) { throw new FileOpException(e); } }
From source file:neembuu.uploader.zip.generator.utils.NUFileUtils.java
/** * Get all the files with the given extension. * @param file The directory where there are all the files. * @param extension The extension of the files to search for. * @return The list of the files.//from w w w . j a v a2s .c o m */ public static Collection<File> listAllFilesWithExt(File file, final String extension) { return FileUtils.listFiles(file, new IOFileFilter() { @Override public boolean accept(File file) { // to prevent old libs compiled by netbeans from getting into. // i know since we are using git this should not happen, but when the code // was been tweaked for testing, this statement was reqired. if (file.getAbsolutePath().replace('/', '\\').contains("\\dist\\")) return false; return FilenameUtils.isExtension(file.getName(), extension); } @Override public boolean accept(File dir, String name) { if (dir.getAbsolutePath().replace('/', '\\').contains("\\dist\\")) return false; return FilenameUtils.isExtension(name, extension); } }, TrueFileFilter.INSTANCE); }
From source file:com.gs.obevo.db.testutil.DirectoryAssert.java
/** * Primitive DB comparison method//from w w w . ja va2 s .c o m * We just compare file names, not the subdirecory structure also * (so this would fail if multiple subdirectories had the same file name) */ public static void assertDirectoriesEqual(File expected, File actual) { MutableList<File> expectedFiles = FastList .newList(FileUtils.listFiles(expected, new WildcardFileFilter("*"), DIR_FILE_FILTER)); expectedFiles = expectedFiles.sortThisBy(toRelativePath(expected)); MutableList<File> actualFiles = FastList .newList(FileUtils.listFiles(actual, new WildcardFileFilter("*"), DIR_FILE_FILTER)); actualFiles = actualFiles.sortThisBy(toRelativePath(actual)); assertEquals( String.format("Directories did not have same # of files:\nExpected: %1$s\nbut was: %2$s", expectedFiles.makeString("\n"), actualFiles.makeString("\n")), expectedFiles.size(), actualFiles.size()); for (int i = 0; i < expectedFiles.size(); i++) { File expectedFile = expectedFiles.get(i); File actualFile = actualFiles.get(i); String expectedFilePath = getRelativePath(expectedFile, expected); String actualFilePath = getRelativePath(actualFile, actual); System.out.println("Comparing" + expectedFilePath + " vs " + actualFilePath); assertEquals("File " + i + " [" + expectedFile + " vs " + actualFile + " does not match paths relative from their roots", expectedFilePath, actualFilePath); FileAssert.assertEquals("Mismatch on file " + expectedFile.getAbsolutePath(), expectedFile, actualFile); } }
From source file:TraverseDirectory.java
public static void recursieTraverseDirectory(String path) throws FileNotFoundException, IOException { File file = new File(path); //It will List all the files and subdirectores file if it is Set to True Collection<File> files = FileUtils.listFiles(file, null, true); for (File file2 : files) { String filter = ".txt"; //Extension for Filtering the file if (file2.toString().endsWith(filter)) { //Function call to Traverse the file with .txt extension traveseFile(file2.toString()); }//from w w w . ja v a 2 s.c om } }
From source file:com.wipro.ats.bdre.tdimport.FileScan.java
public static void scanAndAddToQueue() { try {//from w w w .jav a2 s . c om String scanDir = TDImportRunnableMain.getMonitoredDirName(); LOGGER.debug("Scanning directory: " + scanDir); File dir = new File(scanDir); if (!dir.exists()) { LOGGER.info("Created monitoring dir " + dir + " success=" + dir.mkdirs()); } File arcDir = new File(scanDir + "/" + TDImportRunnableMain.ARCHIVE); if (!arcDir.exists()) { LOGGER.info("Created monitoring dir " + arcDir + " success=" + arcDir.mkdirs()); } // Getting list of files recursively from directory except '_archive' directory Collection<File> listOfFiles = FileUtils.listFiles(dir, new RegexFileFilter(TDImportRunnableMain.getFilePattern()), new RegexFileFilter("^(?:(?!" + TDImportRunnableMain.ARCHIVE + ").)*$")); String fileName = ""; FileCopyInfo fileCopyInfo = null; for (File file : listOfFiles) { fileName = file.getName(); LOGGER.debug("Matched File Pattern by " + fileName); fileCopyInfo = new FileCopyInfo(); fileCopyInfo.setFileName(fileName); fileCopyInfo.setSubProcessId(TDImportRunnableMain.getSubProcessId()); LOGGER.debug("subprocessid in file scan =" + TDImportRunnableMain.getSubProcessId() + " " + fileCopyInfo.getSubProcessId()); fileCopyInfo.setSrcLocation(file.getAbsolutePath()); fileCopyInfo.setTdTable(TDImportRunnableMain.getTdTable()); fileCopyInfo.setFileSize(file.length()); fileCopyInfo.setTimeStamp(file.lastModified()); fileCopyInfo.setTdDB(TDImportRunnableMain.getTdDB()); fileCopyInfo.setTdUserName(TDImportRunnableMain.getTdUserName()); fileCopyInfo.setTdPassword(TDImportRunnableMain.getTdPassword()); fileCopyInfo.setTdDelimiter(TDImportRunnableMain.getTdDelimiter()); fileCopyInfo.setTdTpdid(TDImportRunnableMain.getTdTpdid()); FileMonitor.addToQueue(fileName, fileCopyInfo); } } catch (Exception err) { LOGGER.error("Error in scan directory ", err); throw new BDREException(err); } }
From source file:documentindexsearch.FilingCabinet.java
public void findFiles() { System.out.println("Find Files called"); File dir = new File(this.getDirectory()); String[] fileExtensions = { "txt", "pdf", "pptx", "ppt", "docx", "doc", "rtf", "xls", "xlsx", "vsd" }; this.files = FileUtils.listFiles(dir, fileExtensions, true); }
From source file:com.wipro.ats.bdre.filemon.FileScan.java
public static void scanAndAddToQueue() { try {//from w w w . ja va 2 s . co m String scanDir = FileMonRunnableMain.getMonitoredDirName(); LOGGER.debug("Scanning directory: " + scanDir); File dir = new File(scanDir); if (!dir.exists()) { LOGGER.info("Created monitoring dir " + dir + " success=" + dir.mkdirs()); } File arcDir = new File(scanDir + "/" + FileMonRunnableMain.ARCHIVE); if (!arcDir.exists()) { LOGGER.info("Created monitoring dir " + arcDir + " success=" + arcDir.mkdirs()); } // Getting list of files recursively from directory except '_archive' directory Collection<File> listOfFiles = FileUtils.listFiles(dir, new RegexFileFilter(FileMonRunnableMain.getFilePattern()), new RegexFileFilter("^(?:(?!" + FileMonRunnableMain.ARCHIVE + ").)*$")); String fileName = ""; FileCopyInfo fileCopyInfo = null; for (File file : listOfFiles) { fileName = file.getName(); LOGGER.debug("Matched File Pattern by " + fileName); fileCopyInfo = new FileCopyInfo(); fileCopyInfo.setFileName(fileName); fileCopyInfo.setSubProcessId(FileMonRunnableMain.getSubProcessId()); fileCopyInfo.setServerId(Integer.toString(123461)); fileCopyInfo.setSrcLocation(file.getAbsolutePath()); fileCopyInfo.setDstLocation(file.getParent().replace(FileMonRunnableMain.getMonitoredDirName(), FileMonRunnableMain.getHdfsUploadDir())); fileCopyInfo.setFileHash(DigestUtils.md5Hex(FileUtils.readFileToByteArray(file))); fileCopyInfo.setFileSize(file.length()); fileCopyInfo.setTimeStamp(file.lastModified()); FileMonitor.addToQueue(fileName, fileCopyInfo); } } catch (Exception err) { LOGGER.error("Error in scan directory ", err); throw new BDREException(err); } }
From source file:com.denimgroup.threadfix.framework.impl.dotNetWebForm.AscxFileMappingsFileParser.java
public static Map<String, AscxFile> getMap(File rootDirectory) { if (!rootDirectory.exists() || !rootDirectory.isDirectory()) { throw new IllegalArgumentException( "Invalid directory passed to WebFormsEndpointGenerator: " + rootDirectory); }//from w ww. j a v a 2 s . c om Collection ascxFiles = FileUtils.listFiles(rootDirectory, new FileExtensionFileFilter("ascx"), TrueFileFilter.INSTANCE); Map<String, AscxFile> map = map(); for (Object aspxFile : ascxFiles) { if (aspxFile instanceof File) { File file = (File) aspxFile; String name = file.getName(); String key = name.contains(".") ? name.substring(0, name.indexOf('.')) : name; map.put(key, new AscxFile(file)); } } return map; }
From source file:de.monticore.templateclassgenerator.Modelfinder.java
/** * Finds all models with a certain {@code fileExtension} in the given * {@code modelPath}/*from w ww .j a v a2s . c om*/ * * @param modelPath * @param fileExtension * @return List of all found models */ public static List<String> getModelsInModelPath(File modelPath, String fileExtension) { List<String> models = new ArrayList<String>(); String[] extension = { fileExtension }; Collection<File> files = FileUtils.listFiles(modelPath, extension, true); for (File f : files) { String model = getDotSeperatedFQNModelName(modelPath.getPath(), f.getPath(), fileExtension); if (model.startsWith(TemplateClassGeneratorConstants.TEMPLATE_CLASSES_SETUP_PACKAGE)) { Log.error("0xA700 ATemplate '" + model + "' must not lay in topfolder '" + TemplateClassGeneratorConstants.TEMPLATE_CLASSES_SETUP_PACKAGE + "'"); } else { Log.info("Found model: " + model, "Modelfinder"); models.add(model); } } return models; }
From source file:com.partnet.automation.util.PathUtils.java
/** * Search for a specific file nested inside of a specific path * //from www .j a v a 2s. c om * @param path * - path that contains the file * @param fileName * - file name of the file * @return - the file that was found * * @throws IllegalStateException * - if more then one file with that specific name was found. */ public static File getFileInPath(final String path, final String fileName) { File dir = new File(path); // find the correct file List<File> files = (List<File>) FileUtils.listFiles(dir, FileFilterUtils.nameFileFilter(fileName), TrueFileFilter.TRUE); LOG.debug("Files found: {}", Arrays.asList(files)); if (files.size() != 1) { throw new IllegalStateException(String.format( "Searching for a file '%s' did not result in the correct number of files! Found %d, expected %d", fileName, files.size(), 1)); } return files.get(0); }