List of usage examples for org.apache.commons.io FileUtils isFileNewer
public static boolean isFileNewer(File file, long timeMillis)
File
is newer than the specified time reference. From source file:twister.core.io.input.Tailer.java
/** * Follows changes in the file, calling the TailerListener's handle method for each new line. */// w w w. j a va2 s. c o m public void run() { RandomAccessFile reader = null; try { long last = 0; // The last time the file was checked for changes long position = 0; // position within the file // Open the file while (run && reader == null) { try { reader = new RandomAccessFile(file, "r"); } catch (FileNotFoundException e) { listener.fileNotFound(); } if (reader == null) { try { Thread.sleep(delay); } catch (InterruptedException e) { } } else { // The current position in the file position = end ? file.length() : 0; last = System.currentTimeMillis(); reader.seek(position); } } while (run) { // Check the file length to see if it was rotated long length = file.length(); if (length < position) { // File was rotated listener.fileRotated(); // Reopen the reader after rotation try { // Ensure that the old file is closed iff we re-open it successfully RandomAccessFile save = reader; reader = new RandomAccessFile(file, "r"); position = 0; // close old file explicitly rather than relying on GC picking up previous RAF IOUtils.closeQuietly(save); } catch (FileNotFoundException e) { // in this case we continue to use the previous reader and position values listener.fileNotFound(); } continue; } else { // File was not rotated // See if the file needs to be read again if (length > position) { // The file has more content than it did last time last = System.currentTimeMillis(); position = readLines(reader); } else if (FileUtils.isFileNewer(file, last)) { /* This can happen if the file is truncated or overwritten * with the exact same length of information. In cases like * this, the file position needs to be reset */ position = 0; reader.seek(position); // cannot be null here // Now we can read new lines last = System.currentTimeMillis(); position = readLines(reader); } } try { Thread.sleep(delay); } catch (InterruptedException e) { } } } catch (Exception e) { listener.handle(e); } finally { IOUtils.closeQuietly(reader); } }
From source file:ubic.gemma.core.loader.util.fetcher.FtpArchiveFetcher.java
protected void unPack(final File toUnpack) { FutureTask<Boolean> future = new FutureTask<>(new Callable<Boolean>() { @Override//from ww w . j a v a 2s.co m @SuppressWarnings("synthetic-access") public Boolean call() { File extractedFile = new File(FileTools.chompExtension(toUnpack.getAbsolutePath())); /* * Decide if an existing file is plausibly usable. Err on the side of caution. */ if (allowUseExisting && extractedFile.canRead() && extractedFile.length() >= toUnpack.length() && !FileUtils.isFileNewer(toUnpack, extractedFile)) { AbstractFetcher.log.warn("Expanded file exists, skipping re-expansion: " + extractedFile); return Boolean.TRUE; } if (expander != null) { expander.setSrc(toUnpack); expander.setDest(toUnpack.getParentFile()); expander.perform(); } else if (toUnpack.getAbsolutePath().toLowerCase().endsWith("zip")) { try { FileTools.unZipFiles(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } else { // gzip. try { FileTools.unGzipFile(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } return Boolean.TRUE; } }); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(future); executor.shutdown(); StopWatch s = new StopWatch(); s.start(); while (!future.isDone() && !future.isCancelled()) { try { Thread.sleep(AbstractFetcher.INFO_UPDATE_INTERVAL); } catch (InterruptedException ie) { future.cancel(true); return; } AbstractFetcher.log .info("Unpacking archive ... " + Math.floor(s.getTime() / 1000.0) + " seconds elapsed"); } }
From source file:ubic.gemma.loader.util.fetcher.FtpArchiveFetcher.java
/** * @param newDir/* w w w . j a va 2s . com*/ * @param seekFile */ protected void unPack(final File toUnpack) { FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() { @Override @SuppressWarnings("synthetic-access") public Boolean call() { File extractedFile = new File(FileTools.chompExtension(toUnpack.getAbsolutePath())); /* * Decide if an existing file is plausibly usable. Err on the side of caution. */ if (allowUseExisting && extractedFile.canRead() && extractedFile.length() >= toUnpack.length() && !FileUtils.isFileNewer(toUnpack, extractedFile)) { log.warn("Expanded file exists, skipping re-expansion: " + extractedFile); return Boolean.TRUE; } if (expander != null) { expander.setSrc(toUnpack); expander.setDest(toUnpack.getParentFile()); expander.perform(); } else if (toUnpack.getAbsolutePath().toLowerCase().endsWith("zip")) { try { FileTools.unZipFiles(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } else { // gzip. try { FileTools.unGzipFile(toUnpack.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } return Boolean.TRUE; } }); ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(future); executor.shutdown(); StopWatch s = new StopWatch(); s.start(); while (!future.isDone() && !future.isCancelled()) { try { Thread.sleep(INFO_UPDATE_INTERVAL); } catch (InterruptedException ie) { future.cancel(true); return; } log.info("Unpacking archive ... " + Math.floor(s.getTime() / 1000.0) + " seconds elapsed"); } }
From source file:uk.org.raje.maven.plugin.msbuild.CxxTestGenMojo.java
private boolean upToDate(File targetPath, File testRunnerFile) { if (!testRunnerFile.exists()) { return false; }//from w w w . j a v a2 s .c o m final DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance(); final DirectoryScanner directoryScanner = new DirectoryScanner(); directoryScanner.setIncludes(new String[] { cxxTest.getTestHeaderPattern() }); directoryScanner.setBasedir(targetPath); directoryScanner.scan(); getLog().debug("Test runner last modified " + dateFormat.format(testRunnerFile.lastModified())); for (String fileName : directoryScanner.getIncludedFiles()) { final File file = new File(targetPath, fileName); getLog().debug(fileName + " last modified " + dateFormat.format(file.lastModified())); if (FileUtils.isFileNewer(file, testRunnerFile)) { return false; } } return true; }
From source file:zzhao.code.tailer.Tailer.java
/** * Follows changes in the file, calling the TailerListener's handle method for each new line. *///from ww w . j a v a 2 s .c o m @Override public void run() { RandomAccessFile reader = null; try { long last = 0; // The last time the file was checked for changes long position = 0; // position within the file // Open the file while (run && reader == null) { try { reader = new RandomAccessFile(file, RAF_MODE); } catch (FileNotFoundException e) { listener.fileNotFound(); } if (reader == null) { try { Thread.sleep(delayMillis); } catch (InterruptedException e) { } } else { // The current position in the file position = end ? file.length() : 0; last = file.lastModified(); reader.seek(position); } } while (run) { boolean newer = FileUtils.isFileNewer(file, last); // IO-279, must be done first // Check the file length to see if it was rotated long length = file.length(); if (length < position) { logger.info(String.format("rotated, legth=%s, position=%s", length, position)); // File was rotated listener.fileRotated(); // Reopen the reader after rotation try { // Ensure that the old file is closed iff we re-open it successfully RandomAccessFile save = reader; reader = new RandomAccessFile(file, RAF_MODE); position = 0; // close old file explicitly rather than relying on GC picking up previous // RAF IOUtils.closeQuietly(save); } catch (FileNotFoundException e) { // in this case we continue to use the previous reader and position values listener.fileNotFound(); } continue; } else { // File was not rotated // See if the file needs to be read again if (length > position) { // The file has more content than it did last time position = readLines(reader); last = file.lastModified(); } else if (newer) { logger.info(String.format("newer, legth=%s, position=%s", length, position)); if (resetFilePositionIfOverwrittenWithTheSameLength) { /* * This can happen if the file is truncated or overwritten with the exact same length of * information. In cases like this, the file position needs to be reset */ position = 0; reader.seek(position); // cannot be null here // Now we can read new lines position = readLines(reader); } last = file.lastModified(); } } if (reOpen) { IOUtils.closeQuietly(reader); } try { Thread.sleep(delayMillis); } catch (InterruptedException e) { } if (run && reOpen) { reader = new RandomAccessFile(file, RAF_MODE); reader.seek(position); logger.info(String.format("reopen, legth=%s, position=%s", length, position)); } } } catch (Exception e) { listener.handle(e); } finally { IOUtils.closeQuietly(reader); } }