List of usage examples for java.nio.file.attribute BasicFileAttributeView setTimes
void setTimes(FileTime lastModifiedTime, FileTime lastAccessTime, FileTime createTime) throws IOException;
From source file:Main.java
public static void main(String[] args) { Path path = Paths.get("C:/tutorial/Java/JavaFX", "Topic.txt"); long time = System.currentTimeMillis(); FileTime fileTime = FileTime.fromMillis(time); try {/*from w w w . j a v a 2 s . co m*/ BasicFileAttributeView bv = Files.getFileAttributeView(path, BasicFileAttributeView.class); bv.setTimes(fileTime, fileTime, fileTime); } catch (IOException e) { System.err.println(e); } }
From source file:Test.java
public static void main(String[] args) throws Exception { Path path = Paths.get("C:/home/docs/users.txt"); BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class); BasicFileAttributes attributes = view.readAttributes(); FileTime lastModifedTime = attributes.lastModifiedTime(); FileTime createTime = attributes.creationTime(); long currentTime = Calendar.getInstance().getTimeInMillis(); FileTime lastAccessTime = FileTime.fromMillis(currentTime); view.setTimes(lastModifedTime, lastAccessTime, createTime); System.out.println(attributes.lastAccessTime()); }
From source file:Main.java
public static void main(String[] args) { Path path = Paths.get("C:\\Java_Dev\\test1.txt"); try {//from w w w. jav a2 s. co m BasicFileAttributeView bfv = Files.getFileAttributeView(path, BasicFileAttributeView.class); BasicFileAttributes bfa = bfv.readAttributes(); System.out.format("Size:%s bytes %n", bfa.size()); System.out.format("Creation Time:%s %n", bfa.creationTime()); System.out.format("Last Access Time:%s %n", bfa.lastAccessTime()); FileTime newLastModifiedTime = null; FileTime newLastAccessTime = null; FileTime newCreateTime = FileTime.from(Instant.now()); bfv.setTimes(newLastModifiedTime, newLastAccessTime, newCreateTime); } catch (IOException e) { e.printStackTrace(); } }
From source file:net.mozq.picto.core.ProcessCore.java
private static ProcessDataStatus process(ProcessCondition processCondition, ProcessData processData, Function<ProcessData, ProcessDataStatus> overwriteConfirm) throws IOException { ProcessDataStatus status;//from w w w . j a v a2 s.c om Path destParentPath = processData.getDestPath().getParent(); if (destParentPath != null) { Files.createDirectories(destParentPath); } if (processCondition.isCheckDigest() || (processCondition.isChangeExifDate() && processData.getBaseDate() != null) || processCondition.isRemveExifTagsGps() || processCondition.isRemveExifTagsAll()) { Path destTempPath = null; try { destTempPath = Files.createTempFile(processData.getDestPath().getParent(), processData.getDestPath().getFileName().toString(), null); if (processCondition.isCheckDigest()) { String algorithm = FILE_DIGEST_ALGORITHM; MessageDigest srcMD = newMessageDigest(algorithm); try (InputStream is = new DigestInputStream( new BufferedInputStream(Files.newInputStream(processData.getSrcPath())), srcMD)) { Files.copy(is, destTempPath, OPTIONS_COPY_REPLACE); } byte[] srcDigest = srcMD.digest(); MessageDigest destMD = newMessageDigest(algorithm); try (InputStream is = new DigestInputStream( new BufferedInputStream(Files.newInputStream(destTempPath)), destMD)) { byte[] b = new byte[1024]; while (is.read(b) != -1) { } } byte[] destDigest = destMD.digest(); if (!isSame(srcDigest, destDigest)) { throw new PictoFileDigestMismatchException( Messages.getString("message.error.digest.mismatch")); } } else if (processCondition.isRemveExifTagsAll()) { ExifRewriter exifRewriter = new ExifRewriter(); try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(destTempPath))) { exifRewriter.removeExifMetadata(processData.getSrcPath().toFile(), os); } catch (ImageReadException | ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } else if (processCondition.isChangeExifDate() || processCondition.isRemveExifTagsGps()) { ImageMetadata imageMetadata = getImageMetadata(processData.getSrcPath()); TiffOutputSet outputSet = getOutputSet(imageMetadata); if (outputSet == null) { Files.copy(processData.getSrcPath(), destTempPath, OPTIONS_COPY_REPLACE); } else { if (processCondition.isChangeExifDate()) { SimpleDateFormat exifDateFormat = new SimpleDateFormat(EXIF_DATE_PATTERN); exifDateFormat.setTimeZone(processCondition.getTimeZone()); String exifBaseDate = exifDateFormat.format(processData.getBaseDate()); DecimalFormat exifSubsecFormat = new DecimalFormat(EXIF_SUBSEC_PATTERN); String exifBaseSubsec = exifSubsecFormat .format((int) (processData.getBaseDate().getTime() / 10) % 100); try { TiffOutputDirectory rootDirectory = outputSet.getRootDirectory(); TiffOutputDirectory exifDirectory = outputSet.getExifDirectory(); if (rootDirectory != null) { rootDirectory.removeField(TiffTagConstants.TIFF_TAG_DATE_TIME); rootDirectory.add(TiffTagConstants.TIFF_TAG_DATE_TIME, exifBaseDate); } if (exifDirectory != null) { exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME, exifBaseSubsec); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_ORIGINAL, exifBaseDate); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_ORIGINAL); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_ORIGINAL, exifBaseSubsec); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED); exifDirectory.add(ExifTagConstants.EXIF_TAG_DATE_TIME_DIGITIZED, exifBaseDate); exifDirectory.removeField(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_DIGITIZED); exifDirectory.add(ExifTagConstants.EXIF_TAG_SUB_SEC_TIME_DIGITIZED, exifBaseSubsec); } } catch (ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } if (processCondition.isRemveExifTagsGps()) { outputSet.removeField(ExifTagConstants.EXIF_TAG_GPSINFO); } ExifRewriter exifRewriter = new ExifRewriter(); try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(destTempPath))) { exifRewriter.updateExifMetadataLossless(processData.getSrcPath().toFile(), os, outputSet); } catch (ImageReadException | ImageWriteException e) { throw new PictoFileChangeException(Messages.getString("message.error.edit.file"), e); } } } Path destPath; if (processCondition.getOperationType() == OperationType.Overwrite) { destPath = processData.getSrcPath(); } else { destPath = processData.getDestPath(); } try { Files.move(destTempPath, destPath, OPTIONS_MOVE); if (processCondition.getOperationType() == OperationType.Move) { Files.deleteIfExists(processData.getSrcPath()); } status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { // Overwrite Files.move(destTempPath, destPath, OPTIONS_MOVE_REPLACE); if (processCondition.getOperationType() == OperationType.Move) { Files.deleteIfExists(processData.getSrcPath()); } status = ProcessDataStatus.Success; } } } finally { if (destTempPath != null) { Files.deleteIfExists(destTempPath); } } } else { switch (processCondition.getOperationType()) { case Copy: try { Files.copy(processData.getSrcPath(), processData.getDestPath(), OPTIONS_COPY); status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { Files.copy(processData.getSrcPath(), processData.getDestPath(), OPTIONS_COPY_REPLACE); status = ProcessDataStatus.Success; } } break; case Move: try { Files.move(processData.getSrcPath(), processData.getDestPath(), OPTIONS_MOVE); status = ProcessDataStatus.Success; } catch (FileAlreadyExistsException e) { status = confirmOverwrite(processCondition, processData, overwriteConfirm); if (status == ProcessDataStatus.Processing) { Files.move(processData.getSrcPath(), processData.getDestPath(), OPTIONS_MOVE_REPLACE); status = ProcessDataStatus.Success; } } break; case Overwrite: // NOP status = ProcessDataStatus.Success; break; default: throw new IllegalStateException(processCondition.getOperationType().toString()); } } if (status == ProcessDataStatus.Success) { FileTime creationFileTime = processData.getSrcFileAttributes().creationTime(); FileTime modifiedFileTime = processData.getSrcFileAttributes().lastModifiedTime(); FileTime accessFileTime = processData.getSrcFileAttributes().lastAccessTime(); if (processCondition.isChangeFileCreationDate() || processCondition.isChangeFileModifiedDate() || processCondition.isChangeFileAccessDate()) { if (processData.getBaseDate() != null) { FileTime baseFileTime = FileTime.fromMillis(processData.getBaseDate().getTime()); if (processCondition.isChangeFileCreationDate()) { creationFileTime = baseFileTime; } if (processCondition.isChangeFileModifiedDate()) { modifiedFileTime = baseFileTime; } if (processCondition.isChangeFileAccessDate()) { accessFileTime = baseFileTime; } } } BasicFileAttributeView attributeView = Files.getFileAttributeView(processData.getDestPath(), BasicFileAttributeView.class); attributeView.setTimes(modifiedFileTime, accessFileTime, creationFileTime); } return status; }
From source file:org.sonar.batch.bootstrap.GlobalTempFolderProviderTest.java
private void setFileCreationDate(File f, long time) throws IOException { BasicFileAttributeView attributes = Files.getFileAttributeView(f.toPath(), BasicFileAttributeView.class); FileTime creationTime = FileTime.fromMillis(time); attributes.setTimes(creationTime, creationTime, creationTime); }
From source file:org.cryptomator.frontend.webdav.servlet.DavNode.java
protected void setModificationTime(Instant instant) throws DavException { BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class); if (attrView != null) { try {/* w ww .j av a 2 s .co m*/ attrView.setTimes(FileTime.from(instant), null, null); } catch (IOException e) { throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e); } } }
From source file:org.cryptomator.frontend.webdav.servlet.DavNode.java
protected void setCreationTime(Instant instant) throws DavException { BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class); if (attrView != null) { try {/*from www . j ava2s . com*/ attrView.setTimes(null, null, FileTime.from(instant)); } catch (IOException e) { throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e); } } }
From source file:org.cryptomator.webdav.jackrabbit.AbstractEncryptedNode.java
@Override public void setProperty(DavProperty<?> property) throws DavException { getProperties().add(property);// ww w . j ava 2 s. c om LOG.info("Set property {}", property.getName()); try { final Path path = ResourcePathUtils.getPhysicalPath(this); if (DavPropertyName.CREATIONDATE.equals(property.getName()) && property.getValue() instanceof String) { final String createDateStr = (String) property.getValue(); final FileTime createTime = FileTimeUtils.fromRfc1123String(createDateStr); final BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); attrView.setTimes(null, null, createTime); LOG.info("Updating Creation Date: {}", createTime.toString()); } else if (DavPropertyName.GETLASTMODIFIED.equals(property.getName()) && property.getValue() instanceof String) { final String lastModifiedTimeStr = (String) property.getValue(); final FileTime lastModifiedTime = FileTimeUtils.fromRfc1123String(lastModifiedTimeStr); final BasicFileAttributeView attrView = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); attrView.setTimes(lastModifiedTime, null, null); LOG.info("Updating Last Modified Date: {}", lastModifiedTime.toString()); } } catch (IOException e) { throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:org.cryptomator.frontend.webdav.servlet.DavFolder.java
private void copyInternal(DavNode destination, boolean shallow) throws DavException { assert exists(); assert attr.isPresent(); if (!Files.isDirectory(destination.path.getParent())) { throw new DavException(DavServletResponse.SC_CONFLICT, "Destination's parent doesn't exist."); }// w w w . j a v a2s .co m try { if (shallow && destination instanceof DavFolder) { // http://www.webdav.org/specs/rfc2518.html#copy.for.collections Files.createDirectory(destination.path); BasicFileAttributeView attrView = Files.getFileAttributeView(destination.path, BasicFileAttributeView.class); if (attrView != null) { BasicFileAttributes a = attr.get(); attrView.setTimes(a.lastModifiedTime(), a.lastAccessTime(), a.creationTime()); } } else { Files.walkFileTree(path, new CopyingFileVisitor(path, destination.path, StandardCopyOption.REPLACE_EXISTING)); } } catch (IOException e) { throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e); } }
From source file:org.duracloud.retrieval.mgmt.RetrievalWorker.java
protected void applyTimestamps(ContentStream content, File localFile) { FileTime createTime = convertDateToFileTime(content.getDateCreated()); FileTime lastAccessTime = convertDateToFileTime(content.getDateLastAccessed()); FileTime lastModTime = convertDateToFileTime(content.getDateLastModified()); BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(localFile.toPath(), BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); // If any time value is null, that value is left unchanged try {/*from w w w. ja v a2s .c o m*/ fileAttributeView.setTimes(lastModTime, lastAccessTime, createTime); } catch (IOException e) { logger.error("Error setting timestamps for local file " + localFile.getAbsolutePath() + ": " + e.getMessage(), e); } }