java.io.FileFilter Java Examples

The following examples show how to use java.io.FileFilter. 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: MockFileFinder.java    From synopsys-detect with Apache License 2.0 7 votes vote down vote up
@Override
public List<File> findFiles(final File directoryToSearch, final List<String> filenamePatterns, final int depth, boolean findInsideMatchingDirectories) {
    List<File> found = new ArrayList<>();
    for (int i = 0; i <= depth; i++) {
        if (files.containsKey(i)) {
            List<File> possibles = files.get(i);
            for (String pattern : filenamePatterns) {
                FileFilter fileFilter = new WildcardFileFilter(pattern);
                for (File possible : possibles) {
                    if (fileFilter.accept(possible)) {
                        found.add(possible);
                    }
                }

            }
        }
    }
    return found;
}
 
Example #2
Source File: DeviceUtils.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * 获取CPU个数
 */
public static int getCoreNum() {
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            // Check if filename is "cpu", followed by a single digit number
            if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                return true;
            }
            return false;
        }
    }

    try {
        // Get directory containing CPU info
        File dir = new File("/sys/devices/system/cpu/");
        // Filter to only list the devices we care about
        File[] files = dir.listFiles(new CpuFilter());
        // Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        Log.e(TAG, "getCoreNum", e);
        // Default to return 1 core
        return 1;
    }
}
 
Example #3
Source File: ImportTestAction.java    From apimanager-swagger-promote with Apache License 2.0 6 votes vote down vote up
private void copyImagesAndCertificates(String origConfigFile, TestContext context) {
	File sourceDir = new File(origConfigFile).getParentFile();
	if(!sourceDir.exists()) {
		sourceDir = new File(ImportTestAction.class.getResource(origConfigFile).getFile()).getParentFile();
		if(!sourceDir.exists()) { 
			return;
		}
	}
	FileFilter filter = new WildcardFileFilter(new String[] {"*.crt", "*.jpg", "*.png", "*.pem"});
	try {
		LOG.info("Copy certificates and images from source: "+sourceDir+" into test-dir: '"+testDir+"'");
		FileUtils.copyDirectory(sourceDir, testDir, filter);
	} catch (IOException e) {

	}
}
 
Example #4
Source File: FileUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 删除目录下所有过滤的文件
 * @param dir    目录
 * @param filter 过滤器
 * @return {@code true} 删除成功, {@code false} 删除失败
 */
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (filter == null) return false;
    // dir is null then return false
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example #5
Source File: DirectoryVisitor.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public BreadthFirstDirectoryVisitor(Iterable<File> startingDirectories,
        final FileFilter directoryFilter, FileFilter filter,
        boolean compareCase) {
    this.directoryFilter = directoryFilter == null ? DIRECTORIES
            : new FileFilter() {
                public boolean accept(File pathname) {
                    return pathname.isDirectory()
                            && directoryFilter.accept(pathname);
                }
            };
    this.filter = filter;
    comparator = compareCase ? CASE_SENSITIVE : CASE_INSENSITIVE;
    for (File directory : startingDirectories) {
        if (!directory.isDirectory()) {
            throw new RuntimeException(directory + " is not a directory");
        }
        directoryQueue.addLast(directory);
    }
}
 
Example #6
Source File: FileUtils.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Finds files within a given directory (and optionally its
 * subdirectories). All files found are filtered by an IOFileFilter.
 *
 * @param files the collection of files found.
 * @param directory the directory to search in.
 * @param filter the filter to apply to files and directories.
 * @param includeSubDirectories indicates if will include the subdirectories themselves
 */
private static void innerListFiles(Collection<File> files, File directory,
        IOFileFilter filter, boolean includeSubDirectories) {
    File[] found = directory.listFiles((FileFilter) filter);
    
    if (found != null) {
        for (File file : found) {
            if (file.isDirectory()) {
                if (includeSubDirectories) {
                    files.add(file);
                }
                innerListFiles(files, file, filter, includeSubDirectories);
            } else {
                files.add(file);
            }
        }
    }
}
 
Example #7
Source File: BuildCmd.java    From product-microgateway with Apache License 2.0 6 votes vote down vote up
private boolean isProtosAvailable(String fileLocation) {
    File file = new File(fileLocation);
    if (!file.exists()) {
        return false;
    }
    FilenameFilter protoFilter = (f, name) -> (name.endsWith(".proto"));
    String[] fileNames = file.list(protoFilter);
    if (fileNames != null && fileNames.length > 0) {
        return true;
    }
    //allow the users to have proto definitions inside a directory if required
    FileFilter dirFilter = (f) -> f.isDirectory();
    File[] subDirectories = file.listFiles(dirFilter);
    for (File dir : subDirectories) {
        return isProtosAvailable(dir.getAbsolutePath());
    }
    return false;
}
 
Example #8
Source File: IndexConfiguration.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private Map<File, List<File>> recoverIndexDirectories() {
    final Map<File, List<File>> indexDirectoryMap = new HashMap<>();

    for (final File storageDirectory : repoConfig.getStorageDirectories().values()) {
        final List<File> indexDirectories = new ArrayList<>();
        final File[] matching = storageDirectory.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File pathname) {
                return pathname.isDirectory() && indexNamePattern.matcher(pathname.getName()).matches();
            }
        });

        if (matching != null) {
            for (final File matchingFile : matching) {
                indexDirectories.add(matchingFile);
            }
        }

        indexDirectoryMap.put(storageDirectory, indexDirectories);
    }

    return indexDirectoryMap;
}
 
Example #9
Source File: LoadLevelWindow.java    From Project-16x16 with GNU General Public License v3.0 6 votes vote down vote up
public LoadLevelWindow(SideScroller a, GameplayScene scene) {

		super(a);
		collidableObjects = new ArrayList<CollidableObject>();
		backgroundObjects = new ArrayList<BackgroundObject>();
		picked = "";
		this.scene = scene;
		f = new File(path);
		f.mkdirs();
		File[] files = f.listFiles(new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				String name = pathname.getName().toLowerCase();
				return name.endsWith(".dat") && pathname.isFile();
			}
		});

		list = new List(a, Arrays.stream(files).map(File::getName).toArray(String[]::new), 30);
		list.setSizeH(200);
		list.setPosition(applet.width / 2 + 400, 325);
		list.setConfirmButton("Confirm", applet.width / 2 + 400, 500);
		list.setCancelButton("Cancel", applet.width / 2 + 400, 550);
	}
 
Example #10
Source File: FileUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取目录下所有过滤的文件
 * @param dir         目录
 * @param filter      过滤器
 * @param isRecursive 是否递归进子目录
 * @return 文件链表
 */
public static List<FileList> listFilesInDirWithFilterBean(final File dir, final FileFilter filter, final boolean isRecursive) {
    if (!isDirectory(dir) || filter == null) return null;
    List<FileList> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                FileList fileList;
                if (isRecursive && file.isDirectory()) {
                    List<FileList> subs = listFilesInDirWithFilterBean(file, filter, true);
                    fileList = new FileList(file, subs);
                } else {
                    fileList = new FileList(file);
                }
                list.add(fileList);
            }
        }
    }
    return list;
}
 
Example #11
Source File: PluginUninstaller.java    From Neptune with Apache License 2.0 6 votes vote down vote up
/**
 * 删除遗留的低版本的apk
 */
private static void deleteOldApks(File rootDir, final String packageName) {
    List<File> apkFiles = new ArrayList<>();
    FileFilter fileFilter = new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String name = pathname.getName();
            return name.startsWith(packageName) && name.endsWith(APK_SUFFIX);
        }
    };
    File[] files = rootDir.listFiles(fileFilter);
    if (files != null) {
        for (File file : files) {
            apkFiles.add(file);
        }
    }
    // 删除相关apk文件
    for (File apkFile : apkFiles) {
        if (apkFile.delete()) {
            PluginDebugLog.installFormatLog(TAG, "deleteOldApks %s,  dex %s success!", packageName, apkFile.getAbsolutePath());
        } else {
            PluginDebugLog.installFormatLog(TAG, "deleteOldApks %s, dex %s fail!", packageName, apkFile.getAbsolutePath());
        }
    }
}
 
Example #12
Source File: OrFileFilter.java    From ModTheSpire with MIT License 6 votes vote down vote up
/**
 * <p>Determine whether a file is to be accepted or not, based on the
 * contained filters. The file is accepted if any one of the contained
 * filters accepts it. This method stops looping over the contained
 * filters as soon as it encounters one whose <tt>accept()</tt> method
 * returns <tt>false</tt> (implementing a "short-circuited AND"
 * operation.)</p>
 *
 * <p>If the set of contained filters is empty, then this method
 * returns <tt>true</tt>.</p>
 *
 * @param file  The file to check for acceptance
 *
 * @return <tt>true</tt> if the file matches, <tt>false</tt> if it doesn't
 */
public boolean accept (File file)
{
    boolean accepted = false;

    if (filters.size() == 0)
        accepted = true;

    else
    {
        for (FileFilter filter : filters)
        {
            accepted = filter.accept (file);
            if (accepted)
                break;
        }
    }

    return accepted;
}
 
Example #13
Source File: SkiaPooledImageRegionDecoder.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * Gets the number of cores available in this device, across all processors.
 * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu"
 * @return The number of cores, or 1 if failed to get result
 */
private int getNumCoresOldPhones() {
    class CpuFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            return Pattern.matches("cpu[0-9]+", pathname.getName());
        }
    }
    try {
        File dir = new File("/sys/devices/system/cpu/");
        File[] files = dir.listFiles(new CpuFilter());
        return files.length;
    } catch(Exception e) {
        return 1;
    }
}
 
Example #14
Source File: FileUtil.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * Delete all files that satisfy the filter in directory.
 *
 * @param dir The directory.
 * @param filter The filter.
 * @return {@code true}: success<br>{@code false}: fail
 */
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example #15
Source File: FileUtil.java    From imsdk-android with MIT License 6 votes vote down vote up
/**
 * Return the files that satisfy the filter in directory.
 *
 * @param dir The directory.
 * @param filter The filter.
 * @param isRecursive True to traverse subdirectories, false otherwise.
 * @return the files that satisfy the filter in directory
 */
public static List<File> listFilesInDirWithFilter(final File dir, final FileFilter filter,
                                                  final boolean isRecursive) {
    if (!isDir(dir)) return null;
    List<File> list = new ArrayList<>();
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                list.add(file);
            }
            if (isRecursive && file.isDirectory()) {
                //noinspection ConstantConditions
                list.addAll(listFilesInDirWithFilter(file, filter, true));
            }
        }
    }
    return list;
}
 
Example #16
Source File: RocksDBLookupTableCache.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private Map<String, File[]> getCachedTableSnapshotsFolders(File dbBaseFolder) {
    Map<String, File[]> result = Maps.newHashMap();
    File[] tableFolders = dbBaseFolder.listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.isDirectory();
        }
    });
    if (tableFolders == null) {
        return result;
    }

    for (File tableFolder : tableFolders) {
        String tableName = tableFolder.getName();
        File[] snapshotFolders = tableFolder.listFiles(new FileFilter() {
            @Override
            public boolean accept(File snapshotFile) {
                return snapshotFile.isDirectory();
            }
        });
        result.put(tableName, snapshotFolders);
    }
    return result;
}
 
Example #17
Source File: CompileApi.java    From pampas with Apache License 2.0 6 votes vote down vote up
/**
 * 查找该目录下的所有的jar文件
 *
 * @param jarPath
 * @throws Exception
 */
private String getJarFiles(String jarPath) throws Exception {
    File sourceFile = new File(jarPath);
    // String jars="";
    if (sourceFile.exists()) {// 文件或者目录必须存在
        if (sourceFile.isDirectory()) {// 若file对象为目录
            // 得到该目录下以.java结尾的文件或者目录
            File[] childrenFiles = sourceFile.listFiles(new FileFilter() {
                public boolean accept(File pathname) {
                    if (pathname.isDirectory()) {
                        return true;
                    } else {
                        String name = pathname.getName();
                        if (name.endsWith(".jar") ? true : false) {
                            jars = jars + pathname.getPath() + File.separatorChar;
                            return true;
                        }
                        return false;
                    }
                }
            });
        }
    }
    return jars;
}
 
Example #18
Source File: FileUtil.java    From java-trader with Apache License 2.0 6 votes vote down vote up
/**
 * 递归遍历所有文件
 */
public static List<File> listAllFiles(File dir, FileFilter filter) {
    List<File> result = new ArrayList<>();
    LinkedList<File> dirs = new LinkedList<>();
    dirs.add( dir );
    while(!dirs.isEmpty()) {
        File file = dirs.poll();
        if ( file.isDirectory() ) {
            File[] files = file.listFiles();
            if (files!=null) {
                dirs.addAll(Arrays.asList(files));
            }
            continue;
        }
        if ( filter==null || filter.accept(file) ) {
            result.add(file);
        }
    }
    return result;
}
 
Example #19
Source File: FileUtil.java    From Music-Player with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
public static List<File> listFiles(@NonNull File directory, @Nullable FileFilter fileFilter) {
    List<File> fileList = new LinkedList<>();
    File[] found = directory.listFiles(fileFilter);
    if (found != null) {
        Collections.addAll(fileList, found);
    }
    return fileList;
}
 
Example #20
Source File: TestHelper.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
static FileFilter createFilter(final String extension) {
    return new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String name = pathname.getName();
            if (name.endsWith(extension)) {
                return true;
            }
            return false;
        }
    };
}
 
Example #21
Source File: FileHandlingTools.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * List the files contained in the directory given, that match the given regex
 */
public static File[] listFilesMatchingRegex(String baseDirectory, String regex) {
    return (new File(baseDirectory)).listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile() && pathname.getName().matches(regex);
        }
    });
}
 
Example #22
Source File: FileUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * 删除目录下所有东西
 * @param dir 目录
 * @return {@code true} 删除成功, {@code false} 删除失败
 */
public static boolean deleteAllInDir(final File dir) {
    return deleteFilesInDirWithFilter(dir, new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return true;
        }
    });
}
 
Example #23
Source File: FileUtil.java    From imsdk-android with MIT License 5 votes vote down vote up
/**
 * Return the files in directory.
 *
 * @param dir The directory.
 * @param isRecursive True to traverse subdirectories, false otherwise.
 * @return the files in directory
 */
public static List<File> listFilesInDir(final File dir, final boolean isRecursive) {
    return listFilesInDirWithFilter(dir, new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return true;
        }
    }, isRecursive);
}
 
Example #24
Source File: UIStage.java    From talos with Apache License 2.0 5 votes vote down vote up
public void openProjectAction(final IProject projectType) {
	String defaultLocation = TalosMain.Instance().ProjectController().getLastDir("Open", projectType);
	if(defaultLocation.equals("")) {
		TalosMain.Instance().ProjectController().getLastDir("Save", projectType);
	}
	fileChooser.setDirectory(defaultLocation);

	fileChooser.setMode(FileChooser.Mode.OPEN);
	fileChooser.setMultiSelectionEnabled(false);

	fileChooser.setFileFilter(new FileFilter() {
		@Override
		public boolean accept(File pathname) {
			return pathname.isDirectory() || pathname.getAbsolutePath().endsWith(projectType.getExtension());
		}
	});
	fileChooser.setSelectionMode(FileChooser.SelectionMode.FILES);

	fileChooser.setListener(new FileChooserAdapter() {
		@Override
		public void selected (Array<FileHandle> file) {
			String path = file.first().file().getAbsolutePath();
			TalosMain.Instance().ProjectController().setProject(projectType);
			TalosMain.Instance().ProjectController().loadProject(Gdx.files.absolute(path));
		}
	});

	stage.addActor(fileChooser.fadeIn());
}
 
Example #25
Source File: FileFilterTest.java    From blog with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) {
	File[] files = new File("D:\\8000").listFiles();
	System.out.println(files.length);

	files = new File("D:\\8000").listFiles(new FileFilter() {
		@Override
		public boolean accept(File file) {
			return file.isHidden();
		}
	});
	System.out.println(files.length);

	files = new File("D:\\8000").listFiles(File::isHidden);
	System.out.println(files.length);
}
 
Example #26
Source File: TestHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static FileFilter createFilter(final String extension) {
    return new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            String name = pathname.getName();
            if (name.endsWith(extension)) {
                return true;
            }
            return false;
        }
    };
}
 
Example #27
Source File: ClassUtil0.java    From PeonyFramwork with Apache License 2.0 5 votes vote down vote up
private static File[] filterClassFiles(String pkgPath) {
    if (pkgPath == null) {
        return null;
    }
    // 接收 .class 文件 或 类文件夹  
    return new File(pkgPath).listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            return (file.isFile() && file.getName().endsWith(".class")) || file.isDirectory();
        }
    });
}
 
Example #28
Source File: UIStage.java    From talos with Apache License 2.0 5 votes vote down vote up
public void saveAsProjectAction() {
	IProject projectType = TalosMain.Instance().ProjectController().getProject();
	String defaultLocation = TalosMain.Instance().ProjectController().getLastDir("Save", projectType);
	fileChooser.setDirectory(defaultLocation);

	final String ext = projectType.getExtension();
	fileChooser.setMode(FileChooser.Mode.SAVE);
	fileChooser.setMultiSelectionEnabled(false);
	fileChooser.setFileFilter(new FileFilter() {
		@Override
		public boolean accept(File pathname) {
			return pathname.isDirectory() || pathname.getAbsolutePath().endsWith(ext);
		}
	});
	fileChooser.setSelectionMode(FileChooser.SelectionMode.FILES);

	fileChooser.setListener(new FileChooserAdapter() {
		@Override
		public void selected(Array<FileHandle> file) {
			String path = file.first().file().getAbsolutePath();
			if(!path.endsWith(ext)) {
				if(path.indexOf(".") > 0) {
					path = path.substring(0, path.indexOf("."));
				}
				path += ext;
			}
			FileHandle handle = Gdx.files.absolute(path);
			TalosMain.Instance().ProjectController().saveProject(handle);
		}
	});

	fileChooser.setDefaultFileName(TalosMain.Instance().ProjectController().currentTab.fileName);

	stage.addActor(fileChooser.fadeIn());
}
 
Example #29
Source File: FileHandlingTools.java    From tsml with GNU General Public License v3.0 5 votes vote down vote up
/**
 * List the files contained in the directory given
 */
public static File[] listFiles(String baseDirectory) {
    return (new File(baseDirectory)).listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    });
}
 
Example #30
Source File: IOUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * Get the list of xml files in the bookmark export folder.
 * @return The list of xml files in the bookmark export folder.
 */
public static List<String> getExportedBookmarksFileList() {
	List<String> result = new ArrayList<String>();
	
	File folder = getBookmarksExportFolder();		
	
	if (folder != null) {
		
		FileFilter filter = new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
                   return (pathname.isFile()) &&
                           (pathname.getPath().endsWith(".xml"));
               }
		};
		
		File[] files = folder.listFiles(filter);
		
		for (File file : files) {
			result.add(file.getName());
		}			
	}
	
	Collections.sort(result, new Comparator<String>() {

		@Override
		public int compare(String arg0, String arg1) {				
			return arg1.compareTo(arg0);
		}    		
   	});
	
	return result;
}