java.nio.file.Files.setLastModifiedTime()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(106)

本文整理了Java中java.nio.file.Files.setLastModifiedTime()方法的一些代码示例,展示了Files.setLastModifiedTime()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.setLastModifiedTime()方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:setLastModifiedTime

Files.setLastModifiedTime介绍

暂无

代码示例

代码示例来源:origin: google/guava

/**
 * 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.
 */
@SuppressWarnings("GoodTime") // reading system time without TimeSource
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.
  }
 }
}

代码示例来源:origin: google/j2objc

/**
 * 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.
  }
 }
}

代码示例来源:origin: prestodb/presto

/**
 * 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.
  }
 }
}

代码示例来源:origin: wildfly/wildfly

/**
 * 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.
  }
 }
}

代码示例来源:origin: igniterealtime/Openfire

Files.setLastModifiedTime( dir, Files.getLastModifiedTime( file ) );
Log.debug( "Extracting plugin '{}'...", pluginName );
for ( Enumeration e = zipFile.entries(); e.hasMoreElements(); )

代码示例来源:origin: igniterealtime/Openfire

@Override
  public void run()
  {
    final FileTime now = FileTime.fromMillis( System.currentTimeMillis() );
    for ( final Handler handler : this.server.getChildHandlersByClass( WebAppContext.class ) )
    {
      final File tempDirectory = ((WebAppContext) handler).getTempDirectory();
      try
      {
        Log.debug( "Updating the last modified timestamp of content in Jetty's temporary storage in: {}", tempDirectory);
        Files.walk( tempDirectory.toPath() )
          .forEach( f -> {
            try
            {
              Log.trace( "Setting the last modified timestamp of file '{}' in Jetty's temporary storage to: {}", f, now);
              Files.setLastModifiedTime( f, now );
            }
            catch ( IOException e )
            {
              Log.warn( "An exception occurred while trying to update the last modified timestamp of content in Jetty's temporary storage in: {}", f, e );
            }
          } );
      }
      catch ( IOException e )
      {
        Log.warn( "An exception occurred while trying to update the last modified timestamp of content in Jetty's temporary storage in: {}", tempDirectory, e );
      }
    }
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

private void updateLastModified(final Path... paths) throws IOException {
  for (final Path path : paths) {
    Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000));
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

private void updateLastModified(final Path... paths) throws IOException {
  for (final Path path : paths) {
    Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000));
  }
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

private void updateLastModified(final Path... paths) throws IOException {
  for (final Path path : paths) {
    Files.setLastModifiedTime(path, FileTime.fromMillis(System.currentTimeMillis() + 2000));
  }
}

代码示例来源:origin: syncany/syncany

protected void setLastModified(FileVersion reconstructedFileVersion, File reconstructedFilesAtFinalLocation) {
  // Using Files.setLastModifiedTime() instead of File.setLastModified()  
  // due to pre-1970 issue. See #374 for details.
  
  try {
    FileTime newLastModifiedTime = FileTime.fromMillis(reconstructedFileVersion.getLastModified().getTime());
    Files.setLastModifiedTime(reconstructedFilesAtFinalLocation.toPath(), newLastModifiedTime);
  }
  catch (IOException e) {
    logger.log(Level.WARNING, "Warning: Could not set last modified date for file " + reconstructedFilesAtFinalLocation + "; Ignoring error.", e);
  }        
}

代码示例来源:origin: google/guava

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);
}

代码示例来源:origin: org.apache.logging.log4j/log4j-core

@Before
public void setUp() throws Exception {
  base = Files.createTempDirectory("tempDir", new FileAttribute<?>[0]);
  aaa = Files.createTempFile(base, "aaa", null, new FileAttribute<?>[0]);
  bbb = Files.createTempFile(base, "bbb", null, new FileAttribute<?>[0]);
  ccc = Files.createTempFile(base, "ccc", null, new FileAttribute<?>[0]);
  
  // lastModified granularity is 1 sec(!) on some file systems...
  final long now = System.currentTimeMillis();
  Files.setLastModifiedTime(aaa, FileTime.fromMillis(now));
  Files.setLastModifiedTime(bbb, FileTime.fromMillis(now + 1000));
  Files.setLastModifiedTime(ccc, FileTime.fromMillis(now + 2000));
}

代码示例来源:origin: igniterealtime/Openfire

public boolean reloadPlugin( String pluginName )
{
  Log.debug( "Reloading plugin '{}'..." );
  final Plugin plugin = getPlugin( pluginName );
  if ( plugin == null )
  {
    Log.warn( "Unable to reload plugin '{}'. No such plugin loaded.", pluginName );
    return false;
  }
  final Path path = getPluginPath( plugin );
  if ( path == null )
  {
    // When there's a plugin, there should be a path. If there isn't, our code is buggy.
    throw new IllegalStateException( "Unable to determine installation path of plugin: " + pluginName );
  }
  try
  {
    Files.setLastModifiedTime( path, FileTime.fromMillis( 0 ) );
  }
  catch ( IOException e )
  {
    Log.warn( "Unable to reload plugin '{}'. Unable to reset the 'last modified time' of the plugin path. Try removing and restoring the plugin jar file manually." );
    return false;
  }
  pluginMonitor.runNow( false );
  return true;
}

代码示例来源:origin: MovingBlocks/Terasology

@Test
  public void getNextGamePreviewImagePathOldestFileTest() throws IOException, InterruptedException {
    Files.createDirectories(TMP_PREVIEWS_FOLDER);
    Files.createFile(TMP_PREVIEWS_FOLDER.resolve("1.jpg"));
    Files.createFile(TMP_PREVIEWS_FOLDER.resolve("2.jpg"));
    Files.createFile(TMP_PREVIEWS_FOLDER.resolve("3.jpg"));
    Files.createFile(TMP_PREVIEWS_FOLDER.resolve("4.jpg"));
    Files.createFile(TMP_PREVIEWS_FOLDER.resolve("5.jpg"));

    final Path expectedOldestFile = TMP_PREVIEWS_FOLDER.resolve("3.jpg");

    Files.setLastModifiedTime(expectedOldestFile, FileTime.fromMillis(0));

    final Path imagePath = GamePreviewImageProvider.getNextGamePreviewImagePath(TMP_FOLDER);

    Assert.assertNotNull(imagePath);
    Assert.assertEquals(expectedOldestFile, imagePath);
  }
}

代码示例来源:origin: rakam-io/rakam

private void updateTimeStamp() throws IOException {
  long remoteTimestamp = connection.getLastModified();
  if (remoteTimestamp != 0) {
    Files.setLastModifiedTime(dest, FileTime.fromMillis(remoteTimestamp));
  }
}

代码示例来源:origin: rakam-io/rakam

private void updateTimeStamp() throws IOException {
  long remoteTimestamp = connection.getLastModified();
  if (remoteTimestamp != 0) {
    Files.setLastModifiedTime(dest, FileTime.fromMillis(remoteTimestamp));
  }
}

代码示例来源:origin: rakam-io/rakam

private void updateTimeStamp()
    throws IOException {
  long remoteTimestamp = connection.getLastModified();
  if (remoteTimestamp != 0) {
    Files.setLastModifiedTime(dest, FileTime.fromMillis(remoteTimestamp));
  }
}

代码示例来源:origin: org.eclipse.jgit/org.eclipse.jgit

/**
 * @param file
 * @param time
 * @throws IOException
 */
static void setLastModified(File file, long time) throws IOException {
  Files.setLastModifiedTime(toPath(file), FileTime.fromMillis(time));
}

代码示例来源:origin: dadoonet/fscrawler

/**
 * Test case for https://github.com/dadoonet/fscrawler/issues/379
 * @throws Exception In case something is wrong
 */
@Test
public void test_move_file() throws Exception {
  startCrawler();
  // We should have one doc first
  countTestHelper(new ESSearchRequest().withIndex(getCrawlerName()), 1L, currentTestResourceDir);
  // We move the file
  logger.info(" ---> Moving file roottxtfile.txt to a tmp dir");
  Files.move(currentTestResourceDir.resolve("roottxtfile.txt"),
      rootTmpDir.resolve("roottxtfile.txt"), StandardCopyOption.ATOMIC_MOVE);
  // We expect to have 0 file
  countTestHelper(new ESSearchRequest().withIndex(getCrawlerName()), 0L, currentTestResourceDir);
  // We move the file back
  logger.info(" ---> Moving file roottxtfile.txt from the tmp dir");
  Files.move(rootTmpDir.resolve("roottxtfile.txt"),
      currentTestResourceDir.resolve("roottxtfile.txt"), StandardCopyOption.ATOMIC_MOVE);
  // We need to "touch" the file we just moved
  Files.setLastModifiedTime(currentTestResourceDir.resolve("roottxtfile.txt"), FileTime.from(Instant.now()));
  // We expect to have 1 file
  countTestHelper(new ESSearchRequest().withIndex(getCrawlerName()), 1L, currentTestResourceDir);
}

代码示例来源:origin: iterate-ch/cyberduck

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);
  }
}

相关文章

微信公众号

最新文章

更多