Search in sources :

Example 46 with FileFilter

use of java.io.FileFilter in project druid by druid-io.

the class LocalFileTimestampVersionFinder method mostRecentInDir.

private URI mostRecentInDir(final Path dir, final Pattern pattern) throws IOException {
    long latestModified = Long.MIN_VALUE;
    URI latest = null;
    for (File file : dir.toFile().listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            return pathname.exists() && pathname.isFile() && (pattern == null || pattern.matcher(pathname.getName()).matches());
        }
    })) {
        final long thisModified = file.lastModified();
        if (thisModified >= latestModified) {
            latestModified = thisModified;
            latest = file.toURI();
        }
    }
    return latest;
}
Also used : FileFilter(java.io.FileFilter) URI(java.net.URI) File(java.io.File)

Example 47 with FileFilter

use of java.io.FileFilter in project druid by druid-io.

the class FileTaskLogs method killOlderThan.

@Override
public void killOlderThan(final long timestamp) throws IOException {
    File taskLogDir = config.getDirectory();
    if (taskLogDir.exists()) {
        if (!taskLogDir.isDirectory()) {
            throw new IOException(String.format("taskLogDir [%s] must be a directory.", taskLogDir));
        }
        File[] files = taskLogDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.lastModified() < timestamp;
            }
        });
        for (File file : files) {
            log.info("Deleting local task log [%s].", file.getAbsolutePath());
            FileUtils.forceDelete(file);
            if (Thread.currentThread().isInterrupted()) {
                throw new IOException(new InterruptedException("Thread interrupted. Couldn't delete all tasklogs."));
            }
        }
    }
}
Also used : IOException(java.io.IOException) FileFilter(java.io.FileFilter) File(java.io.File)

Example 48 with FileFilter

use of java.io.FileFilter in project nutz by nutzam.

the class UTF8_BOM method main.

public static void main(String[] args) {
    final byte[] UTF_BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
    final byte[] bom = new byte[3];
    Disks.visitFile(new File("."), new FileVisitor() {

        public void visit(File file) {
            try {
                FileInputStream fis = new FileInputStream(file);
                fis.read(bom);
                if (bom[0] == UTF_BOM[0] && bom[1] == UTF_BOM[1] && bom[2] == UTF_BOM[2]) {
                    System.out.println("Found BOM --> " + file);
                    byte[] data = Streams.readBytes(fis);
                    fis.close();
                    Files.write(file, data);
                    System.out.println("Fixed");
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }, new FileFilter() {

        public boolean accept(File pathname) {
            if (pathname.isDirectory())
                return true;
            return pathname.getName().endsWith(".java") && pathname.length() > 3;
        }
    });
}
Also used : FileVisitor(org.nutz.lang.util.FileVisitor) FileFilter(java.io.FileFilter) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 49 with FileFilter

use of java.io.FileFilter in project superCleanMaster by joyoyao.

the class AppUtil method getNumCores.

/**
     * 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
     */
public static int getNumCores() {
    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 FileFilter() {

            @Override
            public boolean accept(File pathname) {
                // number
                if (Pattern.matches("cpu[0-9]", pathname.getName())) {
                    return true;
                }
                return false;
            }
        });
        // Return the number of cores (virtual CPU devices)
        return files.length;
    } catch (Exception e) {
        e.printStackTrace();
        return 1;
    }
}
Also used : FileFilter(java.io.FileFilter) File(java.io.File) IOException(java.io.IOException)

Example 50 with FileFilter

use of java.io.FileFilter in project tdi-studio-se by Talend.

the class CustomComponentSettingPage method finish.

private void finish(IProgressMonitor... monitorWrap) {
    IProgressMonitor monitor = null;
    if (monitorWrap != null && monitorWrap.length == 1) {
        monitor = monitorWrap[0];
    }
    final IProxyRepositoryFactory prf = CorePlugin.getDefault().getProxyRepositoryFactory();
    if (PluginChecker.isSVNProviderPluginLoaded() && (!sharedAdded.isEmpty() || !backAdded.isEmpty())) {
        RepositoryWorkUnit repositoryWorkUnit = new RepositoryWorkUnit("Update custom components") {

            @Override
            public void run() throws PersistenceException {
                final IWorkspaceRunnable op = new IWorkspaceRunnable() {

                    @Override
                    public void run(IProgressMonitor subMonitor) throws CoreException {
                        ISVNProviderService service = (ISVNProviderService) GlobalServiceRegister.getDefault().getService(ISVNProviderService.class);
                        String projectLabel = pro.getTechnicalLabel();
                        IWorkspace workspace = ResourcesPlugin.getWorkspace();
                        IProject eclipseProject = workspace.getRoot().getProject(projectLabel);
                        String targetRoot = eclipseProject.getLocation().toString() + "/" + ERepositoryObjectType.getFolderName(ERepositoryObjectType.COMPONENTS);
                        File componentFolder = new File(targetRoot);
                        URL url = null;
                        try {
                            if (!componentFolder.exists()) {
                                FilesUtils.createFoldersIfNotExists(targetRoot, false);
                            }
                            Bundle b = Platform.getBundle(IComponentsFactory.COMPONENTS_LOCATION);
                            url = FileLocator.toFileURL(FileLocator.find(b, new Path(""), null));
                            String sourceRoot = url.getFile();
                            // delete share
                            for (IComponent component : backAdded.keySet()) {
                                String componentFullPath = targetRoot + File.separator + component.getName();
                                if (service.isSVNProject(pro)) {
                                    service.svnEclipseHandlerDelete(eclipseProject, pro, componentFullPath);
                                    if (subMonitor != null) {
                                        subMonitor.worked(10);
                                    }
                                } else {
                                    File file = new File(componentFullPath);
                                    if (file != null && file.exists()) {
                                        org.talend.utils.io.FilesUtils.deleteFolder(file, true);
                                    }
                                }
                            }
                            if (!backAdded.isEmpty()) {
                                getCustomComponentSettings().removeAll(backAdded.values());
                            }
                            FileFilter ff = new FileFilter() {

                                @Override
                                public boolean accept(File pathname) {
                                    if (FilesUtils.isSVNFolder(pathname)) {
                                        return false;
                                    }
                                    return true;
                                }
                            };
                            // share
                            for (IComponent component : sharedAdded.keySet()) {
                                String sourcePath = sourceRoot + component.getPathSource() + File.separator + component.getName();
                                File sourceFile = new File(sourcePath);
                                String targetPath = targetRoot + File.separatorChar + component.getName();
                                File targetFile = new File(targetPath);
                                FilesUtils.copyFolder(sourceFile, targetFile, true, ff, null, true, false);
                                if (subMonitor != null) {
                                    subMonitor.worked(10);
                                }
                            }
                        } catch (Exception e) {
                            resetCustomComponentSetting();
                            ExceptionHandler.process(e);
                        }
                        try {
                            prf.saveProject(pro);
                        } catch (PersistenceException e) {
                            ExceptionHandler.process(e);
                        }
                        try {
                            eclipseProject.refreshLocal(IResource.DEPTH_INFINITE, subMonitor);
                        } catch (CoreException e1) {
                            ExceptionHandler.process(e1);
                        }
                    }
                };
                IWorkspace workspace = ResourcesPlugin.getWorkspace();
                try {
                    ISchedulingRule schedulingRule = workspace.getRoot();
                    // the update the project files need to be done in the workspace runnable to avoid all
                    // notification
                    // of changes before the end of the modifications.
                    workspace.run(op, schedulingRule, IWorkspace.AVOID_UPDATE, new NullProgressMonitor());
                } catch (CoreException e) {
                    throw new PersistenceException(e.getCause());
                }
            }
        };
        repositoryWorkUnit.setRefreshRepository(false);
        repositoryWorkUnit.setForceTransaction(true);
        prf.executeRepositoryWorkUnit(repositoryWorkUnit);
        try {
            repositoryWorkUnit.throwPersistenceExceptionIfAny();
        } catch (PersistenceException e) {
            e.printStackTrace();
        }
    }
    if (monitor != null) {
        monitor.done();
    }
    // refresh again after the gui closed .
    try {
        String projectLabel = pro.getTechnicalLabel();
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IProject eclipseProject = workspace.getRoot().getProject(projectLabel);
        eclipseProject.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
    } catch (CoreException e1) {
        ExceptionHandler.process(e1);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Bundle(org.osgi.framework.Bundle) IComponent(org.talend.core.model.components.IComponent) RepositoryWorkUnit(org.talend.repository.RepositoryWorkUnit) ISVNProviderService(org.talend.core.services.ISVNProviderService) IProject(org.eclipse.core.resources.IProject) URL(java.net.URL) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PersistenceException(org.talend.commons.exception.PersistenceException) ISchedulingRule(org.eclipse.core.runtime.jobs.ISchedulingRule) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) PersistenceException(org.talend.commons.exception.PersistenceException) FileFilter(java.io.FileFilter) File(java.io.File) IProxyRepositoryFactory(org.talend.repository.model.IProxyRepositoryFactory)

Aggregations

FileFilter (java.io.FileFilter)281 File (java.io.File)267 ArrayList (java.util.ArrayList)59 IOException (java.io.IOException)53 JarFile (java.util.jar.JarFile)22 URL (java.net.URL)17 Test (org.junit.Test)16 HashMap (java.util.HashMap)15 FileNotFoundException (java.io.FileNotFoundException)11 FilenameFilter (java.io.FilenameFilter)11 HashSet (java.util.HashSet)11 FileInputStream (java.io.FileInputStream)10 Pattern (java.util.regex.Pattern)10 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)10 MalformedURLException (java.net.MalformedURLException)9 URI (java.net.URI)9 Map (java.util.Map)7 Path (org.apache.hadoop.fs.Path)7 NotNull (org.jetbrains.annotations.NotNull)7 Treebank (edu.stanford.nlp.trees.Treebank)6