Example usage for java.nio.file.attribute FileTime from

List of usage examples for java.nio.file.attribute FileTime from

Introduction

In this page you can find the example usage for java.nio.file.attribute FileTime from.

Prototype

public static FileTime from(long value, TimeUnit unit) 

Source Link

Document

Returns a FileTime representing a value at the given unit of granularity.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    long time = System.currentTimeMillis();
    FileTime fileTime = FileTime.from(time, TimeUnit.MILLISECONDS);
    System.out.println(fileTime);

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    long time = System.currentTimeMillis();
    FileTime fileTime = FileTime.fromMillis(time);

    FileTime fileTime1 = FileTime.from(time, TimeUnit.MILLISECONDS);

    System.out.println(fileTime.compareTo(fileTime1));

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    long time = System.currentTimeMillis();
    FileTime fileTime = FileTime.fromMillis(time);

    FileTime fileTime1 = FileTime.from(time, TimeUnit.MILLISECONDS);

    System.out.println(fileTime.equals(fileTime1));

}

From source file:fr.inria.soctrace.tools.ocelotl.core.caches.DichotomyCache.java

/**
 * Remove a cache file. The used policy is to suppress the file which has
 * the oldest accessed time/*www.j  a  v  a  2s.c  o m*/
 */
public void removeCacheFile() {
    // Init with current time
    FileTime oldestDate = FileTime.from(System.currentTimeMillis(), null);
    CacheParameters oldestParam = null;

    for (CacheParameters aCacheParam : cachedDichotomy.keySet()) {
        try {
            // Get the last access to the file
            Path path = cachedDichotomy.get(aCacheParam).toPath();
            BasicFileAttributes attrs;
            attrs = Files.readAttributes(path, BasicFileAttributes.class);
            FileTime currentTime = attrs.lastAccessTime();

            // If the access is older than the current oldest
            if (currentTime.compareTo(oldestDate) < 0) {
                oldestDate = currentTime;
                oldestParam = aCacheParam;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // Delete oldest accessed cache
    if (!cachedDichotomy.get(oldestParam).delete()) {
        logger.debug("[DICHOTOMY CACHE]: Deletion of cache file " + cachedDichotomy.get(oldestParam).getName()
                + " failed.");
    }

    cachedDichotomy.remove(oldestParam);
}

From source file:fr.inria.soctrace.tools.ocelotl.core.caches.DataCache.java

/**
 * Remove a cache file. The used policy is to suppress the file which has
 * the oldest accessed time/*from   ww w.ja va 2s. c om*/
 */
public void removeCacheFile() {
    // Init with current time
    FileTime oldestDate = FileTime.from(System.currentTimeMillis(), null);
    CacheParameters oldestParam = null;

    for (CacheParameters aCacheParam : cachedData.keySet()) {
        try {
            // Get the last access to the file
            Path path = cachedData.get(aCacheParam).toPath();
            BasicFileAttributes attrs;
            attrs = Files.readAttributes(path, BasicFileAttributes.class);
            FileTime currentTime = attrs.lastAccessTime();

            // If the access is older than the current oldest
            if (currentTime.compareTo(oldestDate) < 0) {
                oldestDate = currentTime;
                oldestParam = aCacheParam;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // Delete oldest accessed cache
    if (!cachedData.get(oldestParam).delete()) {
        logger.debug("DataCache: Deletion of cache file " + cachedData.get(oldestParam).getName() + " failed.");
    }

    cachedData.remove(oldestParam);
}

From source file:org.fao.geonet.kernel.harvest.harvester.geonet.Aligner.java

private void addMetadata(final RecordInfo ri, final boolean localRating, String uuid) throws Exception {
    final String id[] = { null };
    final Element md[] = { null };

    //--- import metadata from MEF file

    Path mefFile = retrieveMEF(ri.uuid);

    try {/*from  w w  w.j  ava2s.  co  m*/
        String fileType = "mef";
        MEFLib.Version version = MEFLib.getMEFVersion(mefFile);
        if (version != null && version.equals(MEFLib.Version.V2)) {
            fileType = "mef2";
        }

        IVisitor visitor = fileType.equals("mef2") ? new MEF2Visitor() : new MEFVisitor();

        MEFLib.visit(mefFile, visitor, new IMEFVisitor() {
            public void handleMetadata(Element mdata, int index) throws Exception {
                md[index] = mdata;
            }

            //--------------------------------------------------------------------

            public void handleMetadataFiles(DirectoryStream<Path> files, Element info, int index)
                    throws Exception {
                // Import valid metadata
                Element metadataValidForImport = extractValidMetadataForImport(files, info);

                if (metadataValidForImport != null) {
                    handleMetadata(metadataValidForImport, index);
                }
            }

            //--------------------------------------------------------------------

            public void handleInfo(Element info, int index) throws Exception {

                final Element metadata = md[index];
                String schema = dataMan.autodetectSchema(metadata, null);
                if (info != null && info.getContentSize() != 0) {
                    Element general = info.getChild("general");
                    if (general != null && general.getContentSize() != 0) {
                        Element schemaInfo = general.getChild("schema");
                        if (schemaInfo != null) {
                            schemaInfo.setText(schema);
                        }
                    }
                }
                if (info != null) {
                    id[index] = addMetadata(ri, md[index], info, localRating, uuid);
                }
            }

            //--------------------------------------------------------------------

            public void handlePublicFile(String file, String changeDate, InputStream is, int index)
                    throws IOException {
                if (id[index] == null)
                    return;

                if (log.isDebugEnabled())
                    log.debug("    - Adding remote public file with name:" + file);
                Path pubDir = Lib.resource.getDir(context, "public", id[index]);

                Path outFile = pubDir.resolve(file);
                try (OutputStream os = Files.newOutputStream(outFile)) {
                    BinaryFile.copy(is, os);
                    IO.touch(outFile,
                            FileTime.from(new ISODate(changeDate).getTimeInSeconds(), TimeUnit.SECONDS));
                }
            }

            public void handleFeatureCat(Element md, int index) throws Exception {
                // Feature Catalog not managed for harvesting
            }

            public void handlePrivateFile(String file, String changeDate, InputStream is, int index)
                    throws IOException {
                if (params.mefFormatFull) {
                    if (log.isDebugEnabled())
                        log.debug("    - Adding remote private file with name:" + file
                                + " available for download for user used for harvester.");
                    Path dir = Lib.resource.getDir(context, "private", id[index]);
                    Path outFile = dir.resolve(file);
                    try (OutputStream os = Files.newOutputStream(outFile)) {
                        BinaryFile.copy(is, os);
                        IO.touch(outFile,
                                FileTime.from(new ISODate(changeDate).getTimeInSeconds(), TimeUnit.SECONDS));
                    }
                }
            }
        });
    } catch (Exception e) {
        //--- we ignore the exception here. Maybe the metadata has been removed just now
        if (log.isDebugEnabled())
            log.debug("  - Skipped unretrievable metadata (maybe has been removed) with uuid:" + ri.uuid);
        result.unretrievable++;
        log.error(e);
    } finally {
        try {
            Files.deleteIfExists(mefFile);
        } catch (IOException e) {
            log.warning("Unable to delete mefFile: " + mefFile);
        }
    }
}

From source file:org.fao.geonet.kernel.harvest.harvester.geonet.Aligner.java

private void saveFile(String id, String file, String dir, String changeDate, InputStream is)
        throws IOException {
    Path resourcesDir = Lib.resource.getDir(context, dir, id);
    Path locFile = resourcesDir.resolve(file);

    ISODate remIsoDate = new ISODate(changeDate);
    boolean saveFile;

    if (!Files.exists(locFile)) {
        saveFile = true;/*from   w  ww  .j av  a  2s .  co m*/
    } else {
        ISODate locIsoDate = new ISODate(Files.getLastModifiedTime(locFile).toMillis(), false);
        saveFile = (remIsoDate.timeDifferenceInSeconds(locIsoDate) > 0);
    }

    if (saveFile) {
        if (log.isDebugEnabled()) {
            log.debug("  - Adding remote " + dir + "  file with name:" + file);
        }

        try (OutputStream os = Files.newOutputStream(locFile)) {
            BinaryFile.copy(is, os);
            IO.touch(locFile, FileTime.from(remIsoDate.getTimeInSeconds(), TimeUnit.SECONDS));
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("  - Nothing to do in dir " + dir + " for file with name:" + file);
        }
    }
}