java.io.File.lastModified()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.5k)|赞(0)|评价(0)|浏览(248)

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

File.lastModified介绍

[英]Returns the time when this file was last modified, measured in milliseconds since January 1st, 1970, midnight. Returns 0 if the file does not exist.
[中]返回自1970年1月1日午夜以来上次修改此文件的时间(以毫秒为单位)。如果文件不存在,则返回0。

代码示例

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

public static boolean isOlder(File file, long timeMillis) {
  if (!file.exists()) {
    return false;
  }
  return file.lastModified() < timeMillis;
}

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

public PluginFileDetails(File plugin, boolean bundledPlugin) {
  this.pluginFile = plugin;
  this.bundledPlugin = bundledPlugin;
  pluginFileName = plugin.getName();
  lastModified = plugin.lastModified();
}

代码示例来源:origin: apache/flume

public FileInfo(File file, EventDeserializer deserializer) {
 this.file = file;
 this.length = file.length();
 this.lastModified = file.lastModified();
 this.deserializer = deserializer;
}

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

name         = file.getName();
exists       = file.exists();
directory    = exists && file.isDirectory();
lastModified = exists ? file.lastModified() : 0;
length       = exists && !directory ? file.length() : 0;

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

@Override
public <T extends Serializable> T load(String id, Class<T> type) {
 try {
  File file = new File(dir, id);
  if (!file.exists() || (validTime > 0 && file.lastModified() < new Date().getTime() - validTime)) {
   return null;
  }
  try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
   Object o = in.readObject();
   return o.getClass() == type ? (T) o : null;
  }
 } catch (IOException | ClassNotFoundException e) {
  return null;
 }
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

static void setLastModifiedNow(File file) throws IOException {
  if (file.exists()) {
    long now = System.currentTimeMillis();
    boolean modified = file.setLastModified(now); // on some devices (e.g. Nexus 5) doesn't work
    if (!modified) {
      modify(file);
      if (file.lastModified() < now) {
        // NOTE: apparently this is a known issue (see: http://stackoverflow.com/questions/6633748/file-lastmodified-is-never-what-was-set-with-file-setlastmodified)
        HttpProxyCacheDebuger.printfWarning("Last modified date {} is not set for file {}", new Date(file.lastModified()).toString() + "\n" + file.getAbsolutePath());
      }
    }
  }
}

代码示例来源:origin: stackoverflow.com

File file = new File(filePath);
Date lastModDate = new Date(file.lastModified());
Log.i("File last modified @ : "+ lastModDate.toString());

代码示例来源:origin: apache/storm

/**
 * Deletes jar files in dirLoc older than seconds.
 *
 * @param dirLoc  the location to look in for file
 * @param seconds how old is too old and should be deleted
 */
@VisibleForTesting
public static void cleanInbox(String dirLoc, int seconds) {
  final long now = Time.currentTimeMillis();
  final long ms = Time.secsToMillis(seconds);
  File dir = new File(dirLoc);
  for (File f : dir.listFiles((file) -> file.isFile() && ((file.lastModified() + ms) <= now))) {
    if (f.delete()) {
      LOG.info("Cleaning inbox ... deleted: {}", f.getName());
    } else {
      LOG.error("Cleaning inbox ... error deleting: {}", f.getName());
    }
  }
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Scans the directory looking for source files to be compiled.
 * The results are returned in the class variable compileList
 * @param srcDir the source directory.
 * @param dest   the destination directory.
 * @param mangler the jsp filename mangler.
 * @param files   the file names to mangle.
 */
protected void scanDir(File srcDir, File dest, JspMangler mangler,
            String[] files) {
  long now = Instant.now().toEpochMilli();
  for (String filename : files) {
    File srcFile = new File(srcDir, filename);
    File javaFile = mapToJavaFile(mangler, srcFile, srcDir, dest);
    if (javaFile == null) {
      continue;
    }
    if (srcFile.lastModified() > now) {
      log("Warning: file modified in the future: " + filename,
        Project.MSG_WARN);
    }
    if (isCompileNeeded(srcFile, javaFile)) {
      compileList.addElement(srcFile.getAbsolutePath());
      javaFiles.addElement(javaFile);
    }
  }
}

代码示例来源:origin: apache/kylin

@Override
protected RawResource getResourceImpl(String resPath) throws IOException {
  File f = file(resPath);
  if (f.exists() && f.isFile()) {
    if (f.length() == 0) {
      logger.warn("Zero length file: {}. ", f.getAbsolutePath());
    }
    return new RawResource(resPath, f.lastModified(), new FileInputStream(f));
  } else {
    return null;
  }
}

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

public Watch(File file, String path) {
  this.file = file;
  this.path = path;
  this.exsists = file.exists();
  this.last = exsists ? file.lastModified() : 0;
  if (file.isDirectory()) {
    contents = file.listFiles();
  }
}

代码示例来源:origin: stackoverflow.com

manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
add(new File("inputDirectory"), target);
target.close();
try
 if (source.isDirectory())
    name += "/";
   JarEntry entry = new JarEntry(name);
   entry.setTime(source.lastModified());
   target.putNextEntry(entry);
   target.closeEntry();
 entry.setTime(source.lastModified());
 target.putNextEntry(entry);
 in = new BufferedInputStream(new FileInputStream(source));

代码示例来源:origin: apache/hive

static void writeFile(long templateTime, String outputDir, String classesDir,
   String className, String str) throws IOException {
 File outputFile = new File(outputDir, className + ".java");
 File outputClass = new File(classesDir, className + ".class");
 if (outputFile.lastModified() > templateTime && outputFile.length() == str.length() &&
   outputClass.lastModified() > templateTime) {
  // best effort
  return;
 }
 writeFile(outputFile, str);
}

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

public static Defines createWithFileName(File file) {
  if (file == null) {
    throw new IllegalArgumentException();
  }
  final Defines result = createEmpty();
  result.overrideFilename(file.getName());
  result.environment.put("filedate", new Date(file.lastModified()).toString());
  // result.environment.put("filename", file.getName());
  // result.environment.put("filenameNoExtension", nameNoExtension(file));
  result.environment.put("dirpath", file.getAbsoluteFile().getParentFile().getAbsolutePath().replace('\\', '/'));
  return result;
}

代码示例来源:origin: eclipse-vertx/vert.x

private void addFileToWatchedList(File file) {
 filesToWatch.add(file);
 Map<File, FileInfo> map = new HashMap<>();
 if (file.isDirectory()) {
  // We're watching a directory contents and its children for changes
  File[] children = file.listFiles();
  if (children != null) {
   for (File child : children) {
    map.put(child, new FileInfo(child.lastModified(), child.length()));
    if (child.isDirectory()) {
     addFileToWatchedList(child);
    }
   }
  }
 } else {
  // Not a directory - we're watching a specific file - e.g. a jar
  map.put(file, new FileInfo(file.lastModified(), file.length()));
 }
 fileMap.put(file, map);
}

代码示例来源:origin: apache/ignite

/**
 * Create log file for given file.
 *
 * @param file Log file.
 */
public VisorLogFile(File file) {
  this(file.getAbsolutePath(), file.length(), file.lastModified());
}

代码示例来源:origin: iBotPeaches/Apktool

public static long recursiveModifiedTime(File file) {
  long modified = file.lastModified();
  if (file.isDirectory()) {
    File[] subfiles = file.listFiles();
    for (int i = 0; i < subfiles.length; i++) {
      long submodified = recursiveModifiedTime(subfiles[i]);
      if (submodified > modified) {
        modified = submodified;
      }
    }
  }
  return modified;
}

代码示例来源:origin: apache/storm

/**
   * Build mocked File object with given (or default) attributes for a file.
   */
  public File build() {
    File mockFile = mock(File.class);
    when(mockFile.getName()).thenReturn(fileName);
    when(mockFile.lastModified()).thenReturn(mtime);
    when(mockFile.isFile()).thenReturn(true);
    try {
      when(mockFile.getCanonicalPath()).thenReturn("/mock/canonical/path/to/" + fileName);
    } catch (IOException e) {
      // we're making mock, ignoring...
    }
    when(mockFile.length()).thenReturn(length);
    return mockFile;
  }
}

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

/** @return true if the output file does not yet exist or its last modification date is before the last modification date of
 *         the input file */
static public boolean isModified (String input, String output, String packFileName, Settings settings) {
  String packFullFileName = output;
  if (!packFullFileName.endsWith("/")) {
    packFullFileName += "/";
  }
  // Check against the only file we know for sure will exist and will be changed if any asset changes:
  // the atlas file
  packFullFileName += packFileName;
  packFullFileName += settings.atlasExtension;
  File outputFile = new File(packFullFileName);
  if (!outputFile.exists()) {
    return true;
  }
  File inputFile = new File(input);
  if (!inputFile.exists()) {
    throw new IllegalArgumentException("Input file does not exist: " + inputFile.getAbsolutePath());
  }
  return isModified(inputFile, outputFile.lastModified());
}

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

public StringBuilder getContent() {
  File file = new File(finalFileName);
  if (!file.exists()) {
    throw new RuntimeException("File not found : " + finalFileName);
  }
  
  // 极为重要,否则在开发模式下 isModified() 一直返回 true,缓存一直失效(原因是 lastModified 默认值为 0)
  this.lastModified = file.lastModified();
  
  return loadFile(file, encoding);
}

相关文章

微信公众号

最新文章

更多