Example usage for java.io File lastModified

List of usage examples for java.io File lastModified

Introduction

In this page you can find the example usage for java.io File lastModified.

Prototype

public long lastModified() 

Source Link

Document

Returns the time that the file denoted by this abstract pathname was last modified.

Usage

From source file:de.undercouch.gradle.tasks.download.OnlyIfModifiedTest.java

/**
 * Tests if the plugin doesn't download a file if the timestamp equals
 * the last-modified header/*from   w w w.j a va  2  s  .  co  m*/
 * @throws Exception if anything goes wrong
 */
@Test
public void dontDownloadIfEqual() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong);
    t.dest(dst);
    t.onlyIfModified(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("Hello", dstContents);
}

From source file:com.wavemaker.common.util.IOUtilsTest.java

public void testTouch() throws Exception {

    File f = File.createTempFile("touch", "tmp");
    f.deleteOnExit();//  w  ww  . j  a va2s .  c o m
    assertTrue(f.exists());

    long lastModified = f.lastModified();

    // UNIX counts in seconds
    Thread.sleep(3000);

    IOUtils.touch(f);

    assertTrue(lastModified < f.lastModified());
}

From source file:com.docdoku.server.storage.filesystem.FileStorageProvider.java

public Date getLastModified(BinaryResource binaryResource, String subResourceVirtualPath)
        throws FileNotFoundException {
    File subResourceFile = new File(getSubResourceFolder(binaryResource),
            Tools.unAccent(subResourceVirtualPath));
    if (subResourceFile.exists()) {
        return new Date(subResourceFile.lastModified());
    } else {//from   ww w. ja  v a2s  .c  o  m
        throw new FileNotFoundException(new StringBuilder("Can't find source file to get last modified date ")
                .append(binaryResource.getFullName()).toString());
    }
}

From source file:org.dawnsci.marketplace.controllers.FileController.java

@RequestMapping(value = "/{solution}/**", method = { RequestMethod.GET, RequestMethod.HEAD })
@ResponseBody/*from ww w  . j  a v  a  2s .c om*/
public ResponseEntity<FileSystemResource> getFile(@PathVariable("solution") String solution,
        HttpServletRequest request) {
    String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
    AntPathMatcher apm = new AntPathMatcher();
    path = apm.extractPathWithinPattern(bestMatchPattern, path);
    File file = fileService.getFile(solution, path);
    if (file.exists() && file.isFile()) {
        try {
            String detect = tika.detect(file);
            MediaType mediaType = MediaType.parseMediaType(detect);
            return ResponseEntity.ok().contentLength(file.length()).contentType(mediaType)
                    .lastModified(file.lastModified()).body(new FileSystemResource(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        throw new ResourceNotFoundException();
    }
    return null;
}

From source file:de.undercouch.gradle.tasks.download.OnlyIfNewerTest.java

/**
 * Tests if the plugin doesn't download a file if the timestamp is newer
 * than the last-modified header//w ww .  j  av  a  2  s. c om
 * @throws Exception if anything goes wrong
 */
@Test
public void dontDownloadIfOlder() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong + 1000);
    t.dest(dst);
    t.onlyIfNewer(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong + 1000, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("Hello", dstContents);
}

From source file:de.undercouch.gradle.tasks.download.OnlyIfNewerTest.java

/**
 * Tests if the plugin downloads a file if the timestamp is older than
 * the last-modified header// w  ww.jav a2  s  .  com
 * @throws Exception if anything goes wrong
 */
@Test
public void newerDownload() throws Exception {
    String lm = "Tue, 15 Nov 1994 12:45:26 GMT";
    long expectedlmlong = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH).parse(lm)
            .getTime();
    lastModified = lm;

    Download t = makeProjectAndTask();
    t.src(makeSrc(LAST_MODIFIED));
    File dst = folder.newFile();
    FileUtils.writeStringToFile(dst, "Hello");
    dst.setLastModified(expectedlmlong - 1000);
    t.dest(dst);
    t.onlyIfNewer(true);
    t.execute();

    long lmlong = dst.lastModified();
    assertEquals(expectedlmlong, lmlong);
    String dstContents = FileUtils.readFileToString(dst);
    assertEquals("lm: " + lm, dstContents);
}

From source file:io.liuwei.web.StaticContentServlet.java

/**
 * Content?.//from   www. jav a  2 s. com
 */
private ContentInfo getContentInfo(String contentPath) {
    ContentInfo contentInfo = new ContentInfo();

    String realFilePath = getBaseDir() + contentPath;
    File file = new File(realFilePath);

    contentInfo.file = file;
    contentInfo.contentPath = contentPath;
    contentInfo.fileName = file.getName();
    contentInfo.length = (int) file.length();

    contentInfo.lastModified = file.lastModified();
    contentInfo.etag = "W/\"" + contentInfo.lastModified + "\"";

    contentInfo.mimeType = mimetypesFileTypeMap.getContentType(contentInfo.fileName);

    if ((contentInfo.length >= GZIP_MINI_LENGTH)
            && ArrayUtils.contains(GZIP_MIME_TYPES, contentInfo.mimeType)) {
        contentInfo.needGzip = true;
    } else {
        contentInfo.needGzip = false;
    }

    return contentInfo;
}

From source file:fitnesse.wiki.FileSystemPage.java

private long getLastModifiedTime() throws Exception {
    long lastModifiedTime;

    final File file = new File(getFileSystemPath() + contentFilename);
    if (file.exists()) {
        lastModifiedTime = file.lastModified();
    } else {/*from  ww  w . j a  v  a2s.co m*/
        lastModifiedTime = getClock().currentClockTimeInMillis();
    }
    return lastModifiedTime;
}

From source file:org.cleverbus.admin.services.log.LogParser.java

public File[] getLogFiles(final DateTime date) throws FileNotFoundException {
    File logFolder = new File(logFolderPath);
    if (!logFolder.exists() || !logFolder.canRead()) {
        throw new FileNotFoundException("there is no readable log folder - " + logFolderPath);
    }/* w w w .  ja  v a2 s.  c o m*/

    final String logDateFormatted = FILE_DATE_FORMAT.print(date);
    final long dateMillis = date.getMillis();

    File[] files = logFolder.listFiles(new FileFilter() {
        @Override
        public boolean accept(File file) {
            String name = file.getName();
            return name.endsWith(FILE_EXTENSION) // it's a log file
                    && name.contains(logDateFormatted) // it contains the date in the name
                    && file.lastModified() >= dateMillis; // and it's not older than the search date
        }
    });

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);

    if (files.length == 0) {
        Log.debug("No log files ending with {}, containing {}, modified after {}, at {}", FILE_EXTENSION,
                logDateFormatted, date, logFolderPath);
    } else {
        Log.debug("Found log files for {}: {}", date, files);
    }

    return files;
}