List of usage examples for java.io FilenameFilter FilenameFilter
FilenameFilter
From source file:edu.tsinghua.hotmobi.UploadLogsTask.java
private boolean uploadLogs() { final String uuid = HotMobiLogger.getInstallationSerialId(context); final File logsDir = HotMobiLogger.getLogsDir(context); boolean hasErrors = false; final String todayDir = HotMobiLogger.DATE_FORMAT.format(new Date()); final FilenameFilter filter = new FilenameFilter() { @Override//w ww . j a va 2s . c o m public boolean accept(File dir, String filename) { return !filename.equalsIgnoreCase(todayDir); } }; for (Object dayLogsDirObj : ArrayUtils.nullToEmpty(logsDir.listFiles(filter))) { final File dayLogsDir = (File) dayLogsDirObj; boolean succeeded = true; for (Object logFileObj : ArrayUtils.nullToEmpty(dayLogsDir.listFiles())) { File logFile = (File) logFileObj; FileTypedData body = null; RestHttpResponse response = null; try { final RestHttpRequest.Builder builder = new RestHttpRequest.Builder(); builder.method(POST.METHOD); builder.url("http://www.dnext.xyz/usage/upload"); final List<Pair<String, String>> headers = new ArrayList<>(); headers.add(Pair.create("X-HotMobi-UUID", uuid)); headers.add(Pair.create("X-HotMobi-Date", dayLogsDir.getName())); headers.add(Pair.create("X-HotMobi-FileName", logFile.getName())); builder.headers(headers); body = new FileTypedData(logFile); builder.body(body); response = client.execute(builder.build()); if (response.isSuccessful()) { succeeded &= logFile.delete(); } response.close(); } catch (IOException e) { Log.w(HotMobiLogger.LOGTAG, e); succeeded = false; hasErrors = true; } finally { Utils.closeSilently(body); Utils.closeSilently(response); } } if (succeeded) { AbsLogger.logIfFalse(dayLogsDir.delete(), "Unable to delete log dir"); } } return hasErrors; }
From source file:ejava.projects.edmv.xml.EDmvParserTest.java
public void testParser() throws Exception { File inDir = new File(inputDir); File[] files = inDir.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return (name.startsWith("dmv-") && name.endsWith(".xml")); }//from w w w .ja va2s . c o m }); for (File file : files) { testParser(file.getCanonicalPath()); } }
From source file:eu.annocultor.data.destinations.AbstractFileWritingGraph.java
void cleanAllFileVolumes() { File[] files = environment.getOutputDir().listFiles(new FilenameFilter() { @Override//from ww w. j a v a 2 s. c o m public boolean accept(File dir, String name) { return name.matches(getId() + "\\.\\d+\\." + extension); } }); if (files != null) { for (File file : files) { if (!file.delete()) { log.warn("Failed to delete file " + file.getName()); } } } }
From source file:com.apress.dwrprojects.fileman.FileSystemFunctions.java
/** * Callback method called for each directory encountered. * * @param inDirectory The next directory encountered. * @param inDepth The depth the directory is under the starting * directory./*from w w w .ja va 2s . c o m*/ * @return True if the directory is handed, false if not. */ @SuppressWarnings("unchecked") // Avoids warning on inResults.add(dv) protected boolean handleDirectory(final File inDirectory, final int inDepth, final Collection inResults) { // Only process directories that are direct children of inDirectory. if (inDepth <= 1) { // Don't process root file system directories. if (inDirectory.getParent() != null) { // Don't process the directory if it's the requested directory if (!directory.equals(inDirectory.getPath())) { // Construct DirectoryVO object. DirectoryVO dv = new DirectoryVO(); dv.setName(inDirectory.getName()); dv.setPath(inDirectory.getPath()); // See if the directory has child directories or not. Anonymous inner // class used to determine whether the directory has children or not. String[] childDirectories = inDirectory.list(new FilenameFilter() { public boolean accept(final File inDir, final String inName) { if (inName != inDirectory.getName()) { File f = new File(inDir.getPath() + File.separator + inName); if (f.isDirectory()) { return true; } else { return false; } } else { return false; } } }); if (childDirectories != null && childDirectories.length > 0) { dv.setHasChildren(true); } inResults.add(dv); } } return true; } else { return false; } }
From source file:hudson.maven.AbstractMaven3xBuildTest.java
public void testSimpleMaven3BuildRedeployPublisher() throws Exception { MavenModuleSet m = createMavenProject(); MavenInstallation mavenInstallation = configureMaven3x(); m.setMaven(mavenInstallation.getName()); File repo = createTmpDir();// ww w . j a va 2s . c o m FileUtils.cleanDirectory(repo); m.getReporters().add(new TestReporter()); m.getPublishersList().add(new RedeployPublisher("", repo.toURI().toString(), true, false)); m.setScm(new ExtractResourceSCM(getClass().getResource("maven3-project.zip"))); m.setGoals("clean install"); MavenModuleSetBuild b = buildAndAssertSuccess(m); assertTrue(MavenUtil.maven3orLater(b.getMavenVersionUsed())); File artifactDir = new File(repo, "com/mycompany/app/my-app/1.7-SNAPSHOT/"); String[] files = artifactDir.list(new FilenameFilter() { public boolean accept(File dir, String name) { System.out.println("file name : " + name); return name.endsWith(".jar"); } }); assertTrue("SNAPSHOT exist", !files[0].contains("SNAPSHOT")); assertTrue("file not ended with -1.jar", files[0].endsWith("-1.jar")); }
From source file:com.sarbyn.ath.explorer.AppAnalyzer.java
private File findDefaultResFolder(File resFolder) { File[] f = resFolder.listFiles(new FilenameFilter() { @Override/*from www. j av a 2 s.c om*/ public boolean accept(File dir, String name) { return DEFAULT_TRANSLATION_FOLDER.equals(name); } }); if (f.length == 0) return null; return f[0]; }
From source file:model.manager.FileManager.java
/** * Method to find all xml files in a given directory. * //from ww w . j a v a 2s . c o m * @param rootPath * @return List<String> */ public static String[] getDataExportObjects(String rootPath) { File dir = new File(rootPath); String[] children = dir.list(); FilenameFilter filter = new FilenameFilter() { @Override public boolean accept(File dir1, String name) { return name.endsWith(".xml"); } }; children = dir.list(filter); return children; }
From source file:com.serotonin.m2m2.Main.java
private static void openZipFiles() throws Exception { ProcessEPoll pep = new ProcessEPoll(); try {/*from w w w.j a v a2 s. com*/ new Thread(pep).start(); File[] zipFiles = new File(new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString()) .listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".zip"); } }); if ((zipFiles == null) || (zipFiles.length == 0)) return; for (File file : zipFiles) { if (!file.isFile()) { continue; } ZipFile zip = new ZipFile(file); try { Properties props = getProperties(zip); String moduleName = props.getProperty("name"); if (moduleName == null) { throw new RuntimeException("name not defined in module properties"); } if (!ModuleUtils.validateName(moduleName)) { throw new RuntimeException(new StringBuilder().append("Module name '").append(moduleName) .append("' is invalid").toString()); } File moduleDir = new File( new StringBuilder().append(Common.MA_HOME).append("/web/modules").toString(), moduleName); if (moduleDir.exists()) LOG.info(new StringBuilder().append("Upgrading module ").append(moduleName).toString()); else { LOG.info(new StringBuilder().append("Installing module ").append(moduleName).toString()); } String persistDirs = props.getProperty("persistPaths"); File moduleSaveDir = new File(new StringBuilder().append(moduleDir).append(".save").toString()); if (!org.apache.commons.lang3.StringUtils.isBlank(persistDirs)) { String[] paths = persistDirs.split(","); for (String path : paths) { path = path.trim(); if (!org.apache.commons.lang3.StringUtils.isBlank(path)) { File from = new File(moduleDir, path); File to = new File(moduleSaveDir, path); if (from.exists()) { if (from.isDirectory()) moveDir(from, to); else { FileUtils.moveFile(from, to); } } } } } deleteDir(moduleDir); if (moduleSaveDir.exists()) moveDir(moduleSaveDir, moduleDir); else { moduleDir.mkdirs(); } Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); String name = entry.getName(); File entryFile = new File(moduleDir, name); if (entry.isDirectory()) entryFile.mkdirs(); else { writeFile(entryFile, zip.getInputStream(entry)); } } File moduleWorkDir = new File(moduleDir, "work"); if (moduleWorkDir.exists()) { moveDir(moduleWorkDir, new File(Common.MA_HOME, "work")); } if (HostUtils.isLinux()) { String permissions = props.getProperty("permissions"); if (!org.apache.commons.lang3.StringUtils.isBlank(permissions)) { String[] s = permissions.split(","); for (String permission : s) { setPermission(pep, moduleName, moduleDir, permission); } } } zip.close(); } catch (Exception e) { LOG.warn(new StringBuilder().append("Error while opening zip file ").append(file.getName()) .append(". Is this module built for the core version that you are using? Module ignored.") .toString(), e); } finally { zip.close(); } file.delete(); } } finally { pep.waitForAll(); pep.terminate(); } }
From source file:com.us.servlet.FileManager.java
@Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { // // ww w .j a v a 2 s . com final String fileRoot = AppHelper.KIND_CONFIG.get(FILE_MGR_ROOT).toString(); File root = FileHelper.getFile(AppHelper.APPSET.getFilePath(), fileRoot); PrintWriter out = resp.getWriter(); final String path = req.getParameter("path") != null ? req.getParameter("path") : ""; final String dirType = req.getParameter("dir").toLowerCase();// ??name or size or type String order = req.getParameter("order") != null ? req.getParameter("order").toLowerCase() : "name"; if (!root.exists()) { root.mkdirs(); } String currentUrl = AppHelper.APPSET.getWebPath() + fileRoot + path; String currentDirPath = path; String moveupDirPath = ""; if (!"".equals(path)) { String str = currentDirPath.substring(0, currentDirPath.length() - 1); moveupDirPath = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : ""; } String[] imageType = AppHelper.KIND_CONFIG.get(FILE_IMAGE_TYPE).toString().split(","); // ??.. if (path.indexOf("..") >= 0) { out.println(I118Helper.i118Value("${fileMgr.accessFail}", req)); return; } // ??/ if (!"".equals(path) && !path.endsWith("/")) { out.println(I118Helper.i118Value("${fileMgr.badArgs}", req)); return; } File showDir = FileHelper.getFile(root, currentDirPath); if (!showDir.isDirectory()) { out.println(I118Helper.i118Value("${fileMgr.rootNotExist}", req)); return; } List<Hashtable<String, Object>> fileList = new ArrayList<Hashtable<String, Object>>(); final File[] listFiles = showDir.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { if (FileHelper.getFile(dir, name).isDirectory()) { return true; } final List<String> fileTypeList = Arrays .<String>asList(AppHelper.KIND_CONFIG.get(dirType).toString().split(",")); return fileTypeList.contains(FilenameUtils.getExtension(name)); } }); if (listFiles != null) { for (File file : listFiles) { Hashtable<String, Object> hash = new Hashtable<String, Object>(); String fileName = file.getName(); if (file.isDirectory()) { hash.put("is_dir", true); hash.put("has_file", (file.listFiles() != null)); hash.put("filesize", 0L); hash.put("is_photo", false); hash.put("filetype", ""); } else if (file.isFile()) { String fileExt = FilenameUtils.getExtension(fileName).toLowerCase(); hash.put("is_dir", false); hash.put("has_file", false); hash.put("filesize", file.length()); hash.put("is_photo", Arrays.<String>asList(imageType).contains(fileExt)); hash.put("filetype", fileExt); } hash.put("filename", fileName); hash.put("datetime", DateUtil.format(file.lastModified(), DateUtil.DATETIME_PATTERN)); fileList.add(hash); } } if ("size".equals(order)) { Collections.sort(fileList, new SizeComparator<Hashtable<String, Object>>()); } else if ("type".equals(order)) { Collections.sort(fileList, new TypeComparator<Hashtable<String, Object>>()); } else { Collections.sort(fileList, new NameComparator<Hashtable<String, Object>>()); } KindFileMgr mgr = new KindFileMgr(); mgr.setMoveup_dir_path(moveupDirPath); mgr.setCurrent_dir_path(currentDirPath); mgr.setCurrent_url(currentUrl); mgr.setTotal_count(fileList.size()); mgr.setFile_list(fileList); resp.setContentType("application/json; charset=UTF-8"); out.println(JSONHelper.obj2Json(mgr)); }
From source file:mitm.test.TestUtils.java
public static int getTempFileCount(final String prefix, final String postfix) { String[] files = SystemUtils.getJavaIoTmpDir().list(new FilenameFilter() { @Override//w w w . j a v a 2 s . c o m public boolean accept(File file, String s) { return StringUtils.startsWith(s, prefix) && StringUtils.endsWith(s, postfix); } }); return files != null ? files.length : 0; }