本文整理汇总了Java中java.nio.file.Files.setLastModifiedTime方法的典型用法代码示例。如果您正苦于以下问题:Java Files.setLastModifiedTime方法的具体用法?Java Files.setLastModifiedTime怎么用?Java Files.setLastModifiedTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.setLastModifiedTime方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: flush
import java.nio.file.Files; //导入方法依赖的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);
}
}
示例2: download
import java.nio.file.Files; //导入方法依赖的package包/类
public boolean download(String siaPath, Path destination) {
LOGGER.info("downloading {}", siaPath);
// final String dest = destination.toAbsolutePath().toString();
final FileTime lastModified = SiaFileUtil.getFileTime(siaPath);
final String tempFileName = destination.getFileName().toString() + ".tempdownload";
Path tempFile = destination.getParent().resolve(tempFileName);
final HttpResponse<String> downloadResult = siaCommand(SiaCommand.DOWNLOAD, ImmutableMap.of("destination", tempFile.toAbsolutePath().toString()), siaPath);
final boolean noHosts = checkErrorFragment(downloadResult, NO_HOSTS);
if (noHosts) {
LOGGER.warn("unable to download file {} due to NO_HOSTS ", siaPath);
return false;
}
if (statusGood(downloadResult)) {
try {
Files.setLastModifiedTime(tempFile, lastModified);
Files.move(tempFile, destination, StandardCopyOption.ATOMIC_MOVE);
Files.setLastModifiedTime(destination, lastModified);
} catch (IOException e) {
throw new RuntimeException("unable to do atomic swap of file " + destination);
}
return true;
}
LOGGER.warn("unable to download siaPath {} for an unexpected reason: {} ", siaPath, downloadResult.getBody());
return false;
}
示例3: postVisitDirectory
import java.nio.file.Files; //导入方法依赖的package包/类
@Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// fix up modification time of directory when done
if (exc == null) {
Path newdir = target.resolve(source.relativize(dir));
try {
FileTime time = Files.getLastModifiedTime(dir);
Files.setLastModifiedTime(newdir, time);
} catch (IOException x) {
System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
}
try {
if (operation == Operation.CUT) {
Files.delete(dir);
}
} catch (IOException e) {
System.err.format("Unable to delete directory: %s: %s%n", newdir, e);
}
}
return CONTINUE;
}
示例4: touch
import java.nio.file.Files; //导入方法依赖的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.
}
}
}
示例5: isWritable
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Returns true if the path is writable.
* Acts just like {@link Files#isWritable(Path)}, except won't
* falsely return false for paths on SUBST'd drive letters
* See https://bugs.openjdk.java.net/browse/JDK-8034057
* Note this will set the file modification time (to its already-set value)
* to test access.
*/
@SuppressForbidden(reason = "works around https://bugs.openjdk.java.net/browse/JDK-8034057")
public static boolean isWritable(Path path) throws IOException {
boolean v = Files.isWritable(path);
if (v || Constants.WINDOWS == false) {
return v;
}
// isWritable returned false on windows, the hack begins!!!!!!
// resetting the modification time is the least destructive/simplest
// way to check for both files and directories, and fails early just
// in getting the current value if file doesn't exist, etc
try {
Files.setLastModifiedTime(path, Files.getLastModifiedTime(path));
return true;
} catch (Throwable e) {
return false;
}
}
示例6: test
import java.nio.file.Files; //导入方法依赖的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);
}
示例7: download
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* Download the resource specified by its URI to the target directory using the provided file name.
*/
Path download(URI uri, Path targetDirectory, String targetFileName, Predicate<Path> useTimeStamp) throws IOException {
URL url = requireNonNull(uri, "uri must not be null").toURL();
requireNonNull(targetDirectory, "targetDirectory must be null");
if (requireNonNull(targetFileName, "targetFileName must be null").isEmpty()) {
throw new IllegalArgumentException("targetFileName must be blank");
}
Files.createDirectories(targetDirectory);
Path targetPath = targetDirectory.resolve(targetFileName);
URLConnection urlConnection = url.openConnection();
FileTime urlLastModifiedTime = FileTime.fromMillis(urlConnection.getLastModified());
if (Files.exists(targetPath)) {
if (Files.getLastModifiedTime(targetPath).equals(urlLastModifiedTime)) {
if (Files.size(targetPath) == urlConnection.getContentLengthLong()) {
if (useTimeStamp.test(targetPath)) {
log.log(Level.FINE, "download skipped - using `%s`%n", targetPath);
return targetPath;
}
}
}
Files.delete(targetPath);
}
log.log(Level.FINE, "download `%s` in progress...%n", uri);
try (InputStream sourceStream = url.openStream(); OutputStream targetStream = Files.newOutputStream(targetPath)) {
sourceStream.transferTo(targetStream);
}
Files.setLastModifiedTime(targetPath, urlLastModifiedTime);
log.log(Level.CONFIG, "download `%s` completed%n", uri);
log.info("stored `%s` [timestamp=%s]%n", targetPath, urlLastModifiedTime.toString());
return targetPath;
}
示例8: testTouchTime
import java.nio.file.Files; //导入方法依赖的package包/类
public void testTouchTime() throws IOException {
Path temp = createTempFile();
assertTrue(Files.exists(temp));
Files.setLastModifiedTime(temp, FileTime.fromMillis(0));
assertEquals(0, Files.getLastModifiedTime(temp).toMillis());
MoreFiles.touch(temp);
assertThat(Files.getLastModifiedTime(temp).toMillis()).isNotEqualTo(0);
}
示例9: setTimestamp
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public void setTimestamp(final Path file, final Long modified) throws BackgroundException {
try {
Files.setLastModifiedTime(session.toPath(file), FileTime.from(modified, TimeUnit.MILLISECONDS));
}
catch(IOException e) {
throw new LocalExceptionMappingService().map("Failure to write attributes of {0}", e, file);
}
}
示例10: setModificationDate
import java.nio.file.Files; //导入方法依赖的package包/类
public void setModificationDate(final long timestamp) throws AccessDeniedException {
if(timestamp < 0) {
return;
}
try {
Files.setLastModifiedTime(Paths.get(path), FileTime.fromMillis(timestamp));
}
catch(IOException e) {
throw new LocalAccessDeniedException(String.format("Cannot change timestamp of %s", path), e);
}
}
示例11: setLastModifiedTime
import java.nio.file.Files; //导入方法依赖的package包/类
/**
* altera o longtime da última modificação no arquivo
*
* @param time data no formato longtime
*/
public static void setLastModifiedTime(File file, long time) {
try {
Path path = file.toPath();
FileTime fileTime = FileTime.fromMillis(time);
Files.setLastModifiedTime(path, fileTime);
} catch (IOException ex) {
}
}
示例12: postVisitDirectory
import java.nio.file.Files; //导入方法依赖的package包/类
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// fix up modification time of directory when done
if (exc == null) {
Path newdir = target.resolve(source.relativize(dir));
try {
FileTime time = Files.getLastModifiedTime(dir);
Files.setLastModifiedTime(newdir, time);
} catch (IOException x) {
LOGGER.log(Level.WARNING, "Unable to copy all attributes to: " + newdir, x);
}
}
return CONTINUE;
}
示例13: updateTimeStamp
import java.nio.file.Files; //导入方法依赖的package包/类
private void updateTimeStamp() throws IOException {
long remoteTimestamp = connection.getLastModified();
if (remoteTimestamp != 0) {
Files.setLastModifiedTime(dest, FileTime.fromMillis(remoteTimestamp));
}
}
示例14: testOneModule
import java.nio.file.Files; //导入方法依赖的package包/类
@Test
public void testOneModule(Path base) throws Exception {
Path src = base.resolve("src");
Path m1 = src.resolve("m1x");
Path build = base.resolve("build");
Files.createDirectories(build);
tb.writeJavaFiles(m1,
"module m1x {}",
"package test; public class Test {}");
new JavacTask(tb)
.options("-m", "m1x", "--module-source-path", src.toString(), "-d", build.toString())
.run(Task.Expect.SUCCESS)
.writeAll();
Path moduleInfoClass = build.resolve("m1x/module-info.class");
Path testTestClass = build.resolve("m1x/test/Test.class");
FileTime moduleInfoTimeStamp = Files.getLastModifiedTime(moduleInfoClass);
FileTime testTestTimeStamp = Files.getLastModifiedTime(testTestClass);
Path moduleInfo = m1.resolve("module-info.java");
if (moduleInfoTimeStamp.compareTo(Files.getLastModifiedTime(moduleInfo)) < 0) {
throw new AssertionError("Classfiles too old!");
}
Path testTest = m1.resolve("test/Test.java");
if (testTestTimeStamp.compareTo(Files.getLastModifiedTime(testTest)) < 0) {
throw new AssertionError("Classfiles too old!");
}
Thread.sleep(2000); //timestamps
new JavacTask(tb)
.options("-m", "m1x", "--module-source-path", src.toString(), "-d", build.toString())
.run(Task.Expect.SUCCESS)
.writeAll();
if (!moduleInfoTimeStamp.equals(Files.getLastModifiedTime(moduleInfoClass))) {
throw new AssertionError("Classfile update!");
}
if (!testTestTimeStamp.equals(Files.getLastModifiedTime(testTestClass))) {
throw new AssertionError("Classfile update!");
}
Thread.sleep(2000); //timestamps
Files.setLastModifiedTime(testTest, FileTime.fromMillis(System.currentTimeMillis()));
new JavacTask(tb)
.options("-m", "m1x", "--module-source-path", src.toString(), "-d", build.toString())
.run(Task.Expect.SUCCESS)
.writeAll();
if (!moduleInfoTimeStamp.equals(Files.getLastModifiedTime(moduleInfoClass))) {
throw new AssertionError("Classfile update!");
}
if (Files.getLastModifiedTime(testTestClass).compareTo(Files.getLastModifiedTime(testTest)) < 0) {
throw new AssertionError("Classfiles too old!");
}
}
示例15: setLastModified
import java.nio.file.Files; //导入方法依赖的package包/类
private static void setLastModified(String fileName, long newTime) throws IOException {
Path path = Paths.get(fileName);
FileTime fileTime = FileTime.fromMillis(newTime);
Files.setLastModifiedTime(path, fileTime);
}