Java 文件上次修改返回 0

标签 java file time jcifs

我有以下代码来检查网络驱动器上保存的文件的最后一次修改。

private long determineLastEdit(ILoaderData file) {

    String localDir = "c:\\Software\\log\\"; 
    String localPDF = "empty28.pdf";
    String originDir = "smb:\\ProdName\\ShareName\\Temp\\username\\path\\to\\folder\\";
      //My company remote storage

    File localFile = new File(originDir + localPDF)
      //this does not work

    //File localFile = new File(localDir + localPDF)
      //this works as expected

    Date currentTime = new Date();
    long timeCurrent = currentTime.getTime();
    long timeFile = localFile.lastModified();
      //this returns 0 on remote, correct time on local

    boolean fileEx = localFile.isFile(); //returns false on remote, true on local
    boolean fileTp = localFile.isAbsolute(); //returns false on remote, true on local


    long difference = Math.abs(timeCurrent - timeFile);

    return difference;

}

赋予文件构造函数的参数如下:

smb:\\\ProdName\\\ShareName\\\Temp\\\username\\\path\\\to\\\folder\\\empty28.pdf

但是,lastModified() 方法由于某种原因返回 0,我做错了什么?该文件没有任何类型的锁定,它是常规的(尽管是空 PDF)。

编辑1:我在本地文件上测试了该方法,路径是:

c:\\\Software\\\log\\\empty28.pdf

返回的值是正确的,我怀疑该方法不允许在给定文件上执行,因为它位于网络驱动器上。但是,此检查发生在已授权的一个线程上。不知道错误在哪里。

编辑2:我更新了代码以提供更好的示例。现在,问题似乎出在从网络驱动器读取文件

EDIT3 我尝试使用不同的方法。导入:

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;

并添加代码:

Path path = Paths.get(localDir + localPDF);
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);

同样的结果,本地驱动器工作,远程驱动器不工作。

最佳答案

根据Javadocs方法lastModified:

Returns a long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs.

因此,请检查您传递给文件构造函数的 URL。

关于Java 文件上次修改返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46423608/

相关文章:

java - 数组索引越界错误

java - 如何从文件中检索数据

java - DataInputStream read() 总是返回 -1

python - Pygame time.clock() 奇怪的行为

java - 在 Spring 中进行 Junit 测试自定义过滤器

java - RESTEASY - 返回带有和不带有列表的相同对象

java - Android Recycler View 仅选择一张图片并在所选图片上显示刻度线

java - 我如何用 Java 读取俄语文件?

mysql - MySQL中如何设置夏令时?

javascript - 有人知道这个 Chrome 错误的解决方法吗?