本文整理汇总了Java中java.nio.file.attribute.FileTime类的典型用法代码示例。如果您正苦于以下问题:Java FileTime类的具体用法?Java FileTime怎么用?Java FileTime使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileTime类属于java.nio.file.attribute包,在下文中一共展示了FileTime类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: touch
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
/**
* Like the unix command of the same name, creates an empty file or updates the last modified
* timestamp of the existing file at the given path to the current system time.
*/
public static void touch(Path path) throws IOException {
checkNotNull(path);
try {
Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis()));
} catch (NoSuchFileException e) {
try {
Files.createFile(path);
} catch (FileAlreadyExistsException ignore) {
// The file didn't exist when we called setLastModifiedTime, but it did when we called
// createFile, so something else created the file in between. The end result is
// what we wanted: a new file that probably has its last modified time set to approximately
// now. Or it could have an arbitrary last modified time set by the creator, but that's no
// different than if another process set its last modified time to something else after we
// created it here.
}
}
}
示例2: getValueAt
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if (columnIndex == 0) {
return templates[rowIndex].getName();
} else if (columnIndex == 1) {
return templates[rowIndex].getVersion();
} else if (columnIndex == 2) {
return templates[rowIndex].getShortDescription();
} else if (columnIndex == 3) {
Path temPath = Paths.get(templates[rowIndex].getFile().getAbsolutePath());
try {
BasicFileAttributes view = Files.getFileAttributeView(temPath, BasicFileAttributeView.class).readAttributes();
FileTime creationTime = view.creationTime();
DateFormat df = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
String cTime = df.format(creationTime.toMillis());
return cTime;
} catch (IOException e) {
return "--";
}
} else {
return null;
}
}
示例3: walkClientsForServerFile
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
private void walkClientsForServerFile(File serverFile) throws IOException {
if(!Files.exists(serverFile.getAbsolutePath())) {
return;
}
FileTime modifiedTime = Files.getLastModifiedTime(serverFile.getAbsolutePath());
List<Inspector> inspectors = new ArrayList<>(getServerFileInspectors(serverFile, modifiedTime));
for (Client client : clients.values()) {
File clientFile = client.files.get(serverFile.getId());
if(clientFile != null) {
inspectors.addAll(getClientFileInspectors(clientFile));
}
}
inspect(serverFile, inspectors);
}
示例4: flush
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
public void flush() {
if (!needsFlush) {
return;
}
needsFlush = false;
LOGGER.info("flushing bucket {}", bucketNumber);
try {
synchronized (this) {
final FileChannel openChannel = getOpenChannel();
if (openChannel.isOpen()) {
openChannel.force(wantsTimestampUpdate);
if (!wantsTimestampUpdate) {
Files.setLastModifiedTime(filePath, lastModifiedTime);
} else {
lastModifiedTime = FileTime.from(Instant.now());
}
wantsTimestampUpdate = false;
}
}
} catch (IOException e) {
LOGGER.warn("unable to flush file of bucket {}", bucketNumber);
}
}
示例5: test
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
/**
* Exercise Files.setLastModifiedTime on the given file
*/
void test(Path path) throws Exception {
FileTime now = Files.getLastModifiedTime(path);
FileTime zero = FileTime.fromMillis(0L);
Path result = Files.setLastModifiedTime(path, zero);
assertTrue(result == path);
assertEquals(Files.getLastModifiedTime(path), zero);
result = Files.setLastModifiedTime(path, now);
assertTrue(result == path);
assertEquals(Files.getLastModifiedTime(path), now);
}
示例6: testTimeConversions
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
static void testTimeConversions(long from, long to, long step) {
ZipEntry ze = new ZipEntry("TestExtraTime.java");
for (long time = from; time <= to; time += step) {
ze.setTime(time);
FileTime lastModifiedTime = ze.getLastModifiedTime();
if (lastModifiedTime.toMillis() != time) {
throw new RuntimeException("setTime should make getLastModifiedTime " +
"return the specified instant: " + time +
" got: " + lastModifiedTime.toMillis());
}
if (ze.getTime() != time) {
throw new RuntimeException("getTime after setTime, expected: " +
time + " got: " + ze.getTime());
}
}
}
示例7: setTimes
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
/**
* Sets the {@link Path}'s last modified time and last access time to
* the given valid times.
*
* @param mtime the modification time to set (only if no less than zero).
* @param atime the access time to set (only if no less than zero).
* @throws IOException if setting the times fails.
*/
@Override
public void setTimes(Path p, long mtime, long atime) throws IOException {
try {
BasicFileAttributeView view = Files.getFileAttributeView(
pathToFile(p).toPath(), BasicFileAttributeView.class);
FileTime fmtime = (mtime >= 0) ? FileTime.fromMillis(mtime) : null;
FileTime fatime = (atime >= 0) ? FileTime.fromMillis(atime) : null;
view.setTimes(fmtime, fatime, null);
} catch (NoSuchFileException e) {
throw new FileNotFoundException("File " + p + " does not exist");
}
}
示例8: testBasicFileAttributesToFileStat
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
@Test
public void testBasicFileAttributesToFileStat() {
Instant instant = Instant.ofEpochSecond(424242l, 42);
FileTime ftime = FileTime.from(instant);
BasicFileAttributes attr = Mockito.mock(BasicFileAttributes.class);
Mockito.when(attr.isDirectory()).thenReturn(true);
Mockito.when(attr.lastModifiedTime()).thenReturn(ftime);
Mockito.when(attr.creationTime()).thenReturn(ftime);
Mockito.when(attr.lastAccessTime()).thenReturn(ftime);
Mockito.when(attr.size()).thenReturn(42l);
FileAttributesUtil util = new FileAttributesUtil();
FileStat stat = util.basicFileAttributesToFileStat(attr);
Assertions.assertTrue((FileStat.S_IFDIR & stat.st_mode.intValue()) == FileStat.S_IFDIR);
Assertions.assertEquals(424242l, stat.st_mtim.tv_sec.get());
Assertions.assertEquals(42, stat.st_mtim.tv_nsec.intValue());
Assertions.assertEquals(424242l, stat.st_ctim.tv_sec.get());
Assertions.assertEquals(42, stat.st_ctim.tv_nsec.intValue());
Assumptions.assumingThat(Platform.IS_MAC || Platform.IS_WINDOWS, () -> {
Assertions.assertEquals(424242l, stat.st_birthtime.tv_sec.get());
Assertions.assertEquals(42, stat.st_birthtime.tv_nsec.intValue());
});
Assertions.assertEquals(424242l, stat.st_atim.tv_sec.get());
Assertions.assertEquals(42, stat.st_atim.tv_nsec.intValue());
Assertions.assertEquals(42l, stat.st_size.longValue());
}
示例9: obtainLock
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
@Override
public Lock obtainLock(@NonNull Directory dir, String lockName) throws IOException {
if (!(dir instanceof RedisDirectory)) {
throw new IllegalArgumentException("Expect argument of type [" + RedisDirectory.class.getName() + "]!");
}
Path lockFile = lockFileDirectory.resolve(lockName);
try {
Files.createFile(lockFile);
log.debug("Lock file path = {}", lockFile.toFile().getAbsolutePath());
} catch (IOException ignore) {
//ignore
log.debug("Lock file already exists!");
}
final Path realPath = lockFile.toRealPath();
final FileTime creationTime = Files.readAttributes(realPath, BasicFileAttributes.class).creationTime();
if (LOCK_HELD.add(realPath.toString())) {
FileChannel fileChannel = null;
FileLock lock = null;
try {
fileChannel = FileChannel.open(realPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
lock = fileChannel.tryLock();
if (lock != null) {
return new RedisLock(lock, fileChannel, realPath, creationTime);
} else {
throw new LockObtainFailedException("Lock held by another program: " + realPath);
}
} finally {
if (lock == null) {
IOUtils.closeQuietly(fileChannel);
clearLockHeld(realPath);
}
}
} else {
throw new LockObtainFailedException("Lock held by this virtual machine: " + realPath);
}
}
示例10: check
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
static void check(FileTime mtime, FileTime atime, FileTime ctime,
ZipEntry ze, byte[] extra) {
/*
System.out.printf(" mtime [%tc]: [%tc]/[%tc]%n",
mtime.to(TimeUnit.MILLISECONDS),
ze.getTime(),
ze.getLastModifiedTime().to(TimeUnit.MILLISECONDS));
*/
if (mtime.to(TimeUnit.SECONDS) !=
ze.getLastModifiedTime().to(TimeUnit.SECONDS))
throw new RuntimeException("Timestamp: storing mtime failed!");
if (atime != null &&
atime.to(TimeUnit.SECONDS) !=
ze.getLastAccessTime().to(TimeUnit.SECONDS))
throw new RuntimeException("Timestamp: storing atime failed!");
if (ctime != null &&
ctime.to(TimeUnit.SECONDS) !=
ze.getCreationTime().to(TimeUnit.SECONDS))
throw new RuntimeException("Timestamp: storing ctime failed!");
if (extra != null) {
// if extra data exists, the current implementation put it at
// the end of the extra data array (implementation detail)
byte[] extra1 = ze.getExtra();
if (extra1 == null || extra1.length < extra.length ||
!Arrays.equals(Arrays.copyOfRange(extra1,
extra1.length - extra.length,
extra1.length),
extra)) {
throw new RuntimeException("Timestamp: storing extra field failed!");
}
}
}
示例11: getLayerFile
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
/**
* Finds the file that stores the content BLOB for an application layer.
*
* @param layerType the type of layer
* @param sourceFiles the source files the layer must be built from
* @return the newest cached layer file that matches the {@code layerType} and {@code sourceFiles}
*/
public Path getLayerFile(CachedLayerType layerType, Set<Path> sourceFiles)
throws CacheMetadataCorruptedException {
switch (layerType) {
case DEPENDENCIES:
case RESOURCES:
case CLASSES:
CacheMetadata cacheMetadata = cache.getMetadata();
ImageLayers<CachedLayerWithMetadata> cachedLayers =
cacheMetadata.filterLayers().byType(layerType).bySourceFiles(sourceFiles).filter();
// Finds the newest cached layer for the layer type.
FileTime newestLastModifiedTime = FileTime.from(Instant.MIN);
Path newestLayerFile = null;
for (CachedLayerWithMetadata cachedLayer : cachedLayers) {
FileTime cachedLayerLastModifiedTime = cachedLayer.getMetadata().getLastModifiedTime();
if (cachedLayerLastModifiedTime.compareTo(newestLastModifiedTime) > 0) {
newestLastModifiedTime = cachedLayerLastModifiedTime;
newestLayerFile = cachedLayer.getContentFile();
}
}
return newestLayerFile;
default:
throw new UnsupportedOperationException("Can only find layer files for application layers");
}
}
示例12: eq
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
static void eq(Instant ins, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(ins);
FileTime t2 = FileTime.from(v2, u2);
if (!t1.equals(t2))
throw new RuntimeException("not equal");
if (t1.hashCode() != t2.hashCode())
throw new RuntimeException("hashCodes should be equal");
}
示例13: LayerMetadata
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
LayerMetadata(List<String> sourceFiles, FileTime lastModifiedTime) {
if (sourceFiles.isEmpty()) {
throw new IllegalArgumentException("Source files for application layer cannot be empty");
}
this.sourceFiles = sourceFiles;
this.lastModifiedTime = lastModifiedTime;
}
示例14: checkFileTime
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
static void checkFileTime(long now, long original) {
if (Math.abs(now - original) > PRECISION) {
System.out.format("Extracted to %s, expected to be close to %s%n",
FileTime.fromMillis(now), FileTime.fromMillis(original));
fail();
}
}
示例15: findNewest
import java.nio.file.attribute.FileTime; //导入依赖的package包/类
/**
* Given a filename, of the versioned form "path-version.type" look at all the files matching
* "path.*\.type" and find the one with the newest modified date.
*
* @param path Path of file
* @return the newest version
* @throws IOException If the path is not valid.
*/
public static String findNewest(Path path) throws IOException {
final String pattern = toPattern(path);
return ls(path.getParent(), pattern).sorted((o1, o2) -> {
try {
final FileTime t1 = Files.getLastModifiedTime(o1);
final FileTime t2 = Files.getLastModifiedTime(o2);
return t2.compareTo(t1);
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}).findFirst().get().toString();
}