Example usage for org.apache.commons.io FileUtils iterateFiles

List of usage examples for org.apache.commons.io FileUtils iterateFiles

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils iterateFiles.

Prototype

public static Iterator iterateFiles(File directory, String[] extensions, boolean recursive) 

Source Link

Document

Allows iteration over the files in a given directory (and optionally its subdirectories) which match an array of extensions.

Usage

From source file:com.cheusov.Jrep.java

private static void grep(String[] args) throws Exception {
    if (args.length == 0)
        args = stdinFilenames;//from  www .j a v  a  2  s. c o m

    for (String fileOrDir : args) {
        try {
            Iterator fileIterator;
            File file = new File(fileOrDir);
            boolean isDir = false;
            if (opt_directories != Directories.READ)
                isDir = file.isDirectory();

            if (isDir && opt_directories == Directories.SKIP)
                continue;

            if (isDir && opt_directories == Directories.RECURSE) {
                fileIterator = FileUtils.iterateFiles(file, fileFilter, DirectoryFileFilter.DIRECTORY);
            } else {
                fileIterator = Arrays.asList(fileOrDir).iterator();
            }

            while (fileIterator.hasNext()) {
                Object fileObj = fileIterator.next();
                String filename;
                if (fileObj instanceof String)
                    filename = (String) fileObj;
                else
                    filename = ((File) fileObj).getPath().replaceAll("^^[.]/", "");

                if (filename.equals("-")) {
                    processFile(System.in, label);
                } else {
                    if (fileFilter.accept(new File(filename))) {
                        FileInputStream in = new FileInputStream(filename);
                        processFile(in, filename);
                        in.close();
                    }
                }
            }
        } catch (IOException e) {
            if (!opt_s)
                System.err.println(e.toString());

            exitStatus = 2;
        }
    }
}

From source file:es.bsc.servicess.ide.PackagingUtils.java

/** Copy runtime files for a package of Orchestration Elements.
 * @param runtime Path to the runtime installation
 * @param lib Package lib folder/*from   w  w w .  j ava  2s.  c o m*/
 * @throws CoreException
 */
private static void copyOrchestrationRuntimeFiles(String runtime, IFolder lib) throws CoreException {
    File lib_dir = new File(runtime + "/lib");
    File web_inf_lib = lib.getLocation().toFile();
    List<File> dirs = getRuntimeLibrariesDirs(lib_dir);
    for (File d : dirs) {
        if (d.isDirectory()) {
            Iterator<File> fi = FileUtils.iterateFiles(d, new String[] { "jar" }, false);
            while (fi.hasNext()) {
                File f = fi.next();
                if (!isFileInDiscardList(f)) {
                    try {
                        // System.out.println("Trying to copy File "+
                        // f.getAbsolutePath());
                        FileUtils.copyFileToDirectory(f, web_inf_lib);
                        log.debug(" File copied " + f.getAbsolutePath());
                    } catch (IOException e) {
                        log.error("File " + f.getAbsolutePath() + "could not be copied to "
                                + web_inf_lib.getAbsolutePath(), e);
                    }
                }
            }
        } else
            log.warn("File " + d.getAbsolutePath() + "is not a directory");
    }

}

From source file:es.bsc.servicess.ide.PackagingUtils.java

/** Copy JaxWS libraries to the front-end package
 * @param lib front-end package library folder
 *//*  w w  w  .  ja  va 2  s  . c  o m*/
private static void copyJaxWSLibraries(IFolder lib) {
    File web_inf_lib = lib.getLocation().toFile();
    Bundle b = Platform.getBundle(BUNDLE_NAME);

    try {
        // TODO: Check if correct when installed
        String s = FileLocator.getBundleFile(b).getAbsolutePath() + "/lib/jaxws/";
        File d = new File(s);
        if (d.isDirectory()) {
            Iterator<File> fi = FileUtils.iterateFiles(d, new String[] { "jar" }, false);
            while (fi.hasNext()) {
                File f = fi.next();
                try {
                    FileUtils.copyFileToDirectory(f, web_inf_lib);
                } catch (IOException e) {
                    log.error("File " + f.getAbsolutePath() + "could not be copied to "
                            + web_inf_lib.getAbsolutePath(), e);
                }

            }
        } else {
            log.warn("File " + d.getAbsolutePath() + " is not a directory");
        }
    } catch (IOException e) {
        log.error("Failing to locate bundle file", e);
        e.printStackTrace();
    }

}

From source file:me.mast3rplan.phantombot.PhantomBot.java

/**
 * Backup the database, keeping so many days.
 *///  w ww . j a  v  a  2  s . co m
private void doBackupSQLiteDB() {

    if (!dataStoreType.equals("sqlite3store")) {
        return;
    }

    ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    service.scheduleAtFixedRate(() -> {
        SimpleDateFormat datefmt = new SimpleDateFormat("ddMMyyyy.hhmmss");
        datefmt.setTimeZone(TimeZone.getTimeZone(timeZone));
        String timestamp = datefmt.format(new Date());

        dataStore.backupSQLite3("phantombot.auto.backup." + timestamp + ".db");

        try {
            Iterator dirIterator = FileUtils.iterateFiles(new File("./dbbackup"),
                    new WildcardFileFilter("phantombot.auto.*"), null);
            while (dirIterator.hasNext()) {
                File backupFile = (File) dirIterator.next();
                if (FileUtils.isFileOlder(backupFile,
                        System.currentTimeMillis() - (86400000 * backupSQLiteKeepDays))) {
                    FileUtils.deleteQuietly(backupFile);
                }
            }
        } catch (Exception ex) {
            com.gmt2001.Console.err.println("Failed to clean up database backup directory: " + ex.getMessage());
        }
    }, 0, backupSQLiteHourFrequency, TimeUnit.HOURS);
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

public void perforceSync(RepoDetail repodetail, String baseDir, String projectName, String flag)
        throws ConnectionException, RequestException {
    String url = repodetail.getRepoUrl();
    String userName = repodetail.getUserName();
    String password = repodetail.getPassword();
    String stream = repodetail.getStream();
    try {//from   w  ww.  j ava  2s  .  c  o  m
        IOptionsServer server = ServerFactory.getOptionsServer("p4java://" + url, null, null);
        server.connect();
        server.setUserName(userName);
        if (password != "") {
            server.login(password);
        }
        IClient client = new Client();
        client.setName(projectName);
        if (flag.equals("update")) {
            String[] rootArr = baseDir.split(projectName);
            String root = rootArr[0].substring(0, rootArr[0].length() - 1);
            client.setRoot(root);
        } else {
            client.setRoot(baseDir);
        }
        client.setServer(server);
        server.setCurrentClient(client);
        ClientViewMapping tempMappingEntry = new ClientViewMapping();
        tempMappingEntry.setLeft(stream + "/...");
        tempMappingEntry.setRight("//" + projectName + "/...");
        ClientView clientView = new ClientView();
        clientView.addEntry(tempMappingEntry);
        try {
            String[] arr = repodetail.getStream().split("//");
            String[] arr1 = arr[1].split("/");
            client.setStream("//" + arr1[0] + "/" + arr1[1]);
            client.setClientView(clientView);
            client.setOptions(new ClientOptions("noallwrite clobber nocompress unlocked nomodtime normdir"));
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new RequestException();
        }
        if (client != null) {
            List<IFileSpec> syncList = client.sync(FileSpecBuilder.makeFileSpecList(stream + "/..."),
                    new SyncOptions());
            for (IFileSpec fileSpec : syncList) {
                if (fileSpec != null) {
                    if (fileSpec.getOpStatus() == FileSpecOpStatus.VALID) {

                    } else {
                        System.err.println(fileSpec.getStatusMessage());
                    }
                }
            }
        }
        IOFileFilter filter = new IOFileFilter() {
            @Override
            public boolean accept(File arg0, String arg1) {
                return true;
            }

            @Override
            public boolean accept(File arg0) {
                return true;
            }
        };
        Iterator<File> iterator = FileUtils.iterateFiles(new File(baseDir), filter, filter);
        while (iterator.hasNext()) {
            File file = iterator.next();
            file.setWritable(true);
        }

    } catch (RequestException rexc) {
        System.err.println(rexc.getDisplayString());
        rexc.printStackTrace();
        throw new RequestException();
    } catch (P4JavaException jexc) {
        System.err.println(jexc.getLocalizedMessage());
        jexc.printStackTrace();
        throw new ConnectionException();
    } catch (Exception exc) {
        System.err.println(exc.getLocalizedMessage());
        exc.printStackTrace();
    }

}

From source file:net.sourceforge.seqware.pipeline.workflowV2.WorkflowV2Utility.java

/**
 * Locate and parse metadata information and return a map representation
 *
 * @param bundle//  w  w  w.  j  a va  2  s.co m
 * @return map if the parse is successful, null if not
 */
public static Map<String, String> parseMetaInfo(File bundle) {
    final String bundlePath = bundle.getAbsolutePath();
    //parse metadata.xml to Map<String,String>
    @SuppressWarnings("unchecked") //safe to use <File>
    Iterator<File> it = FileUtils.iterateFiles(bundle, new String[] { "xml" }, true);
    if (!it.hasNext()) {
        return null;
    }

    File metadataFile = null;
    while (it.hasNext()) {
        File file = it.next();
        if (file.getName().equals("metadata.xml")) {
            metadataFile = file;
            break;
        }
    }

    if (metadataFile == null) {
        return null;
    }
    return parseMetadataInfo(metadataFile, bundlePath);
}

From source file:nl.mpi.oai.harvester.cycle.XMLOverviewTest.java

@Test
/**//  www  .j a v  a 2 s  .c  om
 * Test rotating overview files
 */
public void testOverviewRotate() {

    // get the overview from a test XML overview file
    final XMLOverview xmlOverview = new XMLOverview(TestHelper.getFile("/OverviewNormalMode.xml"));

    /* Instead of rotating the overview file itself, test by rotating a
       copy of this file. Therefore, try to save a copy of the overview
       in a temporary folder first.
     */
    try {
        // create a new temporary file
        final File newFile = temporaryFolder.newFile("CopyOfNormalModeFile.xml");

        // save the overview in the temporary file, creating a copy
        xmlOverview.save(newFile);

    } catch (IOException e) {
        fail();
        e.printStackTrace();
    }

    /* Now rotate the copy. This will create a file with a date and
       timestamp, and a new file containing the overview.
     */
    boolean done = xmlOverview.rotateAndSave();

    if (!done) {
        fail();
    }

    // create a filter for finding the overview XML files
    String[] allowedExtensions = new String[] { "xml" };
    IOFileFilter filter = new SuffixFileFilter(allowedExtensions, IOCase.SENSITIVE);

    // create an iterator based on the filter
    Iterator iterator = FileUtils.iterateFiles(temporaryFolder.getRoot(), filter, null);

    // iterate over the temporary files
    File file1 = (File) iterator.next();
    File file2 = (File) iterator.next();

    if (iterator.hasNext()) {
        // there should only be two files in the temporary folder
        fail();
    }

    // determine which file is the rotated file and which is the new file
    File rotatedFile, newFile;

    int atIndex = file1.getPath().lastIndexOf(" at ");

    if (atIndex < 0) {
        // did not find it in this file
        atIndex = file2.getPath().lastIndexOf(" at ");
        if (atIndex < 0) {
            // did not find it in this file either
            rotatedFile = null;
            newFile = null;
            fail();
        } else {
            rotatedFile = file2;
            newFile = file1;
        }
    } else {
        rotatedFile = file1;
        newFile = file2;
    }

    // determine the index of the first character in the timestamp
    int first = atIndex + 4;

    // determine the index of the last character in the timestamp
    int last = rotatedFile.getPath().lastIndexOf("xml") - 1;

    // get the timestamp from the rotated file
    String timeStamp = rotatedFile.getPath().substring(first, last);

    // the timestamp should be before now
    DateTime rotatedAt = new DateTime(timeStamp);
    assertTrue(rotatedAt.isBeforeNow());

    // the path of the files up to the timestamp should be equal
    String partOfRotated = rotatedFile.getPath().substring(0, atIndex);
    String partOfNew = newFile.getPath().substring(0, atIndex);
    assertTrue(partOfRotated.equals(partOfNew));

    // both files should have equal content
    try {
        assertTrue(FileUtils.contentEquals(rotatedFile, newFile));
    } catch (IOException e) {
        fail();
        e.printStackTrace();
    }
}

From source file:nl.sidn.pcap.load.LoaderThread.java

private void scan() {
    LOGGER.info("Scan for pcap files in: " + inputDir);
    File f = null;/*from  w  w w .j ava  2s. c o  m*/
    try {
        f = FileUtils.getFile(inputDir);
    } catch (Exception e) {
        throw new RuntimeException("input dir error", e);
    }
    Iterator<File> files = FileUtils.iterateFiles(f, new String[] { "pcap.gz" }, false);
    while (files.hasNext()) {
        File pcap = files.next();
        String fqn = pcap.getAbsolutePath();
        if (!processedFiles.contains(fqn) && !inputFiles.contains(fqn)) {
            inputFiles.add(fqn);
        }
    }
    //sort the files by name, tcp streams and udp fragmentation may overlap multiple files.
    //so ordering is important.
    Collections.sort(inputFiles);
    for (String file : inputFiles) {
        LOGGER.info("Found file: " + file);
    }
}

From source file:nl.sidn.pcap.util.FileUtil.java

public static int countFiles(String inputDir) {
    LOGGER.info("Scan for pcap files in: " + inputDir);
    File f = FileUtils.getFile(inputDir);
    Iterator<File> files = FileUtils.iterateFiles(f, new String[] { "pcap.gz" }, false);
    int filecount = 0;
    while (files.hasNext()) {
        files.next();//  ww  w .  j  a  v a 2 s .  c o  m
        filecount++;
    }
    return filecount;
}

From source file:nz.co.fortytwo.signalk.processor.StorageProcessor.java

public StorageProcessor() throws IOException {
    storageHandler = new JsonStorageHandler();
    // make sure we check and re-attach objects in the storage dir
    if (logger.isDebugEnabled())
        logger.debug("Checking storage objects at:" + storageDir.getAbsolutePath());

    Iterator<File> files = FileUtils.iterateFiles(storageDir, new String[] { "json" }, true);
    while (files.hasNext()) {
        File f = files.next();/*w w  w.j a v  a 2  s. c  o  m*/
        if (logger.isDebugEnabled())
            logger.debug("Checking file:" + f);
        Json json = Json.read(FileUtils.readFileToString(f));
        if (json != null && json.has(JsonStorageHandler.PARENT_PATH)) {
            String path = json.at(JsonStorageHandler.PARENT_PATH).asString();
            if (signalkModel.get(path) == null) {
                Map<String, Object> map = json.asMap();
                for (String key : map.keySet()) {
                    signalkModel.getFullData().put(path + dot + key, map.get(key));
                }
                if (logger.isDebugEnabled())
                    logger.debug("Attached storage object at:" + path);
            }
        }
    }
}