Java Code Examples for org.apache.commons.io.FileUtils#listFilesAndDirs()

The following examples show how to use org.apache.commons.io.FileUtils#listFilesAndDirs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectImpl.java    From japi with MIT License 6 votes vote down vote up
@Override
public List<IPackage> getPackages() {
    String masterProjectActionPath = JapiClient.getConfig().getPrefixPath() + JapiClient.getConfig().getProjectJavaPath() + JapiClient.getConfig().getPostfixPath() + "/" + JapiClient.getConfig().getActionReletivePath();
    File actionFold = new File(masterProjectActionPath);
    if (!actionFold.exists()) {
        throw new JapiException(masterProjectActionPath + " fold not exists.");
    }
    final IOFileFilter dirFilter = FileFilterUtils.asFileFilter(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    });
    Collection<File> folds = FileUtils.listFilesAndDirs(actionFold, dirFilter, TrueFileFilter.INSTANCE);
    List<IPackage> packages = new ArrayList<>(folds.size());
    for (File fold : folds) {
        if (!fold.getAbsolutePath().equals(actionFold.getAbsolutePath())) {
            PackageImpl packageImpl = new PackageImpl();
            packageImpl.setPackageFold(fold);
            packages.add(packageImpl);
        }
    }
    return packages;
}
 
Example 2
Source File: MavenRepositoryDeployer.java    From maven-repository-tools with Eclipse Public License 1.0 6 votes vote down vote up
public static Collection<File> getLeafDirectories( File repoPath ) 
{
    // Using commons-io, if performance or so is a problem it might be worth looking at the Java 8 streams API
    // e.g. http://blog.jooq.org/2014/01/24/java-8-friday-goodies-the-new-new-io-apis/
    // not yet though..
   Collection<File> subDirectories =
        FileUtils.listFilesAndDirs( repoPath, DirectoryFileFilter.DIRECTORY,
            VisibleDirectoryFileFilter.DIRECTORY );
    Collection<File> leafDirectories = new ArrayList<File>();
    for ( File subDirectory : subDirectories )
    {
        if ( isLeafVersionDirectory( subDirectory ) && subDirectory != repoPath )
        {
            leafDirectories.add( subDirectory );
        }
    }
    return leafDirectories;
}
 
Example 3
Source File: RocksDBStateBackendTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisposeDeletesAllDirectories() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	Collection<File> allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());
	try {
		ValueStateDescriptor<String> kvId =
			new ValueStateDescriptor<>("id", String.class, null);

		kvId.initializeSerializerUnlessSet(new ExecutionConfig());

		ValueState<String> state =
			backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.update("Hello");

		// more than just the root directory
		assertTrue(allFilesInDbDir.size() > 1);
	} finally {
		IOUtils.closeQuietly(backend);
		backend.dispose();
	}
	allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());

	// just the root directory left
	assertEquals(1, allFilesInDbDir.size());
}
 
Example 4
Source File: RocksDBStateBackendTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisposeDeletesAllDirectories() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	Collection<File> allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());
	try {
		ValueStateDescriptor<String> kvId =
			new ValueStateDescriptor<>("id", String.class, null);

		kvId.initializeSerializerUnlessSet(new ExecutionConfig());

		ValueState<String> state =
			backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.update("Hello");

		// more than just the root directory
		assertTrue(allFilesInDbDir.size() > 1);
	} finally {
		IOUtils.closeQuietly(backend);
		backend.dispose();
	}
	allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());

	// just the root directory left
	assertEquals(1, allFilesInDbDir.size());
}
 
Example 5
Source File: DumpStorageTask.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
	try {
		logger.print("schedule dump storage task start.");
		DumpStorage action = new DumpStorage();
		action.execute(Config.currentNode().dumpStorage().path());
		if (Config.currentNode().dumpStorage().size() > 0) {
			File dir = new File(Config.base(), "local/dump");
			List<File> list = new ArrayList<>();
			if (dir.exists() && dir.isDirectory()) {
				for (File f : FileUtils.listFilesAndDirs(dir, FalseFileFilter.FALSE, new RegexFileFilter(
						"^dumpStorage_[1,2][0,9][0-9][0-9][0,1][0-9][0-3][0-9][0-5][0-9][0-5][0-9][0-5][0-9]$"))) {
					if (dir != f) {
						list.add(f);
					}
				}
				list = list.stream().sorted(Comparator.comparing(File::getName).reversed())
						.collect(Collectors.toList());
				if (list.size() > Config.currentNode().dumpStorage().size()) {
					for (int i = Config.currentNode().dumpStorage().size(); i < list.size(); i++) {
						File file = list.get(i);
						logger.print("dumpStorageTask delete{}.", file.getAbsolutePath());
						FileUtils.forceDelete(file);
					}
				}
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 6
Source File: DumpDataTask.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
	try {
		logger.print("schedule dump data task start.");
		DumpData action = new DumpData();
		action.execute(Config.currentNode().dumpData().path());
		if (Config.currentNode().dumpData().size() > 0) {
			File dir = new File(Config.base(), "local/dump");
			List<File> list = new ArrayList<>();
			if (dir.exists() && dir.isDirectory()) {
				for (File f : FileUtils.listFilesAndDirs(dir, FalseFileFilter.FALSE, new RegexFileFilter(
						"^dumpData_[1,2][0,9][0-9][0-9][0,1][0-9][0-3][0-9][0-5][0-9][0-5][0-9][0-5][0-9]$"))) {
					if (dir != f) {
						list.add(f);
					}
				}
				list = list.stream().sorted(Comparator.comparing(File::getName).reversed())
						.collect(Collectors.toList());
				if (list.size() > Config.currentNode().dumpData().size()) {
					for (int i = Config.currentNode().dumpData().size(); i < list.size(); i++) {
						File file = list.get(i);
						logger.print("dumpDataTask delete:{}.", file.getAbsolutePath());
						FileUtils.forceDelete(file);
					}
				}
			}
		}
	} catch (Exception e) {
		throw new JobExecutionException(e);
	}
}
 
Example 7
Source File: FileTools.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public static void extractConfig(File fzip) throws IOException 
{
	IOFileFilter fileFilter1 =   FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter("logs", null));
	IOFileFilter fileFilter2 =   FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter("data", null));
	IOFileFilter exceptFilter =   FileFilterUtils.and(fileFilter1, fileFilter2 );
	
	
	try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fzip))) {
		for(File f : FileUtils.listFilesAndDirs(MTGConstants.CONF_DIR, FileFileFilter.FILE, exceptFilter))
			addFile(f,out);
	}

}
 
Example 8
Source File: RocksDBStateBackendTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisposeDeletesAllDirectories() throws Exception {
	AbstractKeyedStateBackend<Integer> backend = createKeyedBackend(IntSerializer.INSTANCE);
	Collection<File> allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());
	try {
		ValueStateDescriptor<String> kvId =
			new ValueStateDescriptor<>("id", String.class, null);

		kvId.initializeSerializerUnlessSet(new ExecutionConfig());

		ValueState<String> state =
			backend.getPartitionedState(VoidNamespace.INSTANCE, VoidNamespaceSerializer.INSTANCE, kvId);

		backend.setCurrentKey(1);
		state.update("Hello");

		// more than just the root directory
		assertTrue(allFilesInDbDir.size() > 1);
	} finally {
		IOUtils.closeQuietly(backend);
		backend.dispose();
	}
	allFilesInDbDir =
		FileUtils.listFilesAndDirs(new File(dbPath), new AcceptAllFilter(), new AcceptAllFilter());

	// just the root directory left
	assertEquals(1, allFilesInDbDir.size());
}
 
Example 9
Source File: SrcTreeGenerator.java    From sahagin-java with Apache License 2.0 5 votes vote down vote up
private void addToClassPathList(List<String> classPathList, String[] classPathArray) {
    for (String classPath : classPathArray) {
        if (classPath == null || classPath.trim().equals("")) {
            continue;
        }
        String classPathWithoutPrefix;
        if (classPath.startsWith("file:")) {
            // class path in the jar MANIFEST sometimes has this form of class path
            classPathWithoutPrefix = classPath.substring(5);
        } else {
            classPathWithoutPrefix = classPath;
        }
        String absClassPath = new File(classPathWithoutPrefix).getAbsolutePath();

        if (absClassPath.endsWith(".jar")) {
            if (!classPathList.contains(absClassPath)) {
                classPathList.add(absClassPath);
                addToClassPathListFromJarManifest(classPathList, new File(absClassPath));
            }
        } else if (absClassPath.endsWith(".zip")) {
            if (!classPathList.contains(absClassPath)) {
                classPathList.add(absClassPath);
            }
        } else {
            File classPathFile = new File(absClassPath);
            if (classPathFile.isDirectory()) {
                // needs to add all sub directories
                // since SrcTreeGenerator does not search classPathEntry sub directories
                // TODO should add jar file in the sub directories ??
                Collection<File> subDirCollection = FileUtils.listFilesAndDirs(
                        classPathFile, FileFilterUtils.directoryFileFilter(), FileFilterUtils.trueFileFilter());
                for (File subDir : subDirCollection) {
                    if (!classPathList.contains(subDir.getAbsolutePath())) {
                        classPathList.add(subDir.getAbsolutePath());
                    }
                }
            }
        }
    }
}
 
Example 10
Source File: RMFileUtils.java    From repositoryminer with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves all the sub directories from a root directory.
 * 
 * @param path
 *            the root path.
 * @return the sub directories.
 */
public static List<String> getAllDirsAsString(String path) {
	List<File> dirs = (List<File>) FileUtils.listFilesAndDirs(new File(path), new NotFileFilter(TrueFileFilter.INSTANCE),
			DirectoryFileFilter.DIRECTORY);
	List<String> dirsNames = new ArrayList<>();
	
	for (File f : dirs) {
		dirsNames.add(f.getAbsolutePath());
	}
	return dirsNames;
}
 
Example 11
Source File: UpdateManager.java    From secrecy with Apache License 2.0 5 votes vote down vote up
void version32to53() {
    Collection folders = FileUtils.listFilesAndDirs(Storage.getRoot(), FalseFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
    for (Object folderObject : folders) { //Search for dirs in root
        File folder = (File) folderObject;
        if (new File(folder, ".vault").exists() || !new File(folder, ".nomedia").exists()) {
            appendlog("\n" + folder.getAbsolutePath() + " is 5.x or not a vault, skip");
            continue; //The whole thing should be skipped because vault is in 5.x standard.
        }
        appendlog("\n" + folder.getAbsolutePath() + " is pre-5.x");
        Collection files = FileUtils.listFiles(folder, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
        //walks the whole file tree, find out files that do not have encoded file names
        //and encode them.
        for (Object file : files) {
            File realFile = (File) file;
            if (".nomedia".equals(realFile.getName()))
                continue;
            String fileName = FilenameUtils.removeExtension(realFile.getName());
            fileName = fileName.replace("_thumb", "");
            try {
                Base64Coder.decodeString(fileName);
            } catch (IllegalArgumentException e) {
                String encodedFileName = Base64Coder.encodeString(fileName);
                fileName = realFile.getAbsolutePath().replace(fileName, encodedFileName);
                realFile.renameTo(new File(fileName));
            }
        }
    }
    version53to60();
}
 
Example 12
Source File: FindFiles.java    From butterfly with MIT License 4 votes vote down vote up
@Override
protected TUExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) {
    final File searchRootFolder = getAbsoluteFile(transformedAppFolder, transformationContext);

    String _pathRegex = pathRegex;
    if (pathRegex != null && File.separatorChar != '/') {
        _pathRegex = pathRegex.replace('/', File.separatorChar);
    }
    final String normalizedPathRegex = _pathRegex;

    IOFileFilter filter = new AbstractFileFilter() {
        public boolean accept(File file) {
            if ((file.isFile() && !includeFiles) || (file.isDirectory() && !includeFolders)) {
                return false;
            }
            if (nameRegex != null && !file.getName().matches(nameRegex)) {
                return false;
            }
            if (normalizedPathRegex != null) {
                String relativePath = getRelativePath(searchRootFolder, file.getParentFile());
                if (!relativePath.matches(normalizedPathRegex)) {
                    return false;
                }
            }
            return true;
        }
    };

    Collection<File> files = new ArrayList<>();
    if (includeFiles) {
        files = FileUtils.listFiles(searchRootFolder, filter, (recursive ? TrueFileFilter.INSTANCE : null));
    }
    if (includeFolders) {
        Collection<File> folders = new ArrayList<>();
        Collection<File> allFolders = FileUtils.listFilesAndDirs(searchRootFolder, new NotFileFilter(TrueFileFilter.INSTANCE), (recursive ? TrueFileFilter.INSTANCE : DirectoryFileFilter.DIRECTORY));
        allFolders.remove(searchRootFolder);
        for (File folder : allFolders) {
            if (!recursive && !folder.getParentFile().equals(searchRootFolder)) {
                continue;
            }
            if (filter.accept(folder)) {
                folders.add(folder);
            }
        }
        files.addAll(folders);
    }

    TUExecutionResult result;

    if(files.isEmpty() && warnIfNoFilesFound) {
        result = TUExecutionResult.warning(this, "No files have been found", new ArrayList<>(files));
    } else if(files.isEmpty() && errorIfNoFilesFound) {
        result = TUExecutionResult.error(this, new TransformationUtilityException("No files have been found"), new ArrayList<>(files));
    } else {
        result = TUExecutionResult.value(this, new ArrayList<>(files));
        if (files.isEmpty()) {
            result.setDetails("No files have been found");
        }
    }

    return result;
}
 
Example 13
Source File: PackageBuilder.java    From spoofax with Apache License 2.0 4 votes vote down vote up
private Collection<File> findFiles(File directory) {
    if(!directory.isDirectory()) {
        return Collections.emptyList();
    }
    return FileUtils.listFilesAndDirs(directory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
}
 
Example 14
Source File: RMFileUtils.java    From repositoryminer with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieves all the sub directories from a root directory.
 * 
 * @param path
 *            the root path.
 * @return the sub directories.
 */
public static List<File> getAllDirs(String path) {
	return (List<File>) FileUtils.listFilesAndDirs(new File(path), new NotFileFilter(TrueFileFilter.INSTANCE),
			DirectoryFileFilter.DIRECTORY);
}