Java文件最后修改的返回0



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

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,我在做什么错?该文件没有任何锁,它是常规的(Altough空的PDF)。

edit1:我在本地文件上测试了该方法,路径为:

c:\Software\log\empty28.pdf

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

edit2:我更新了代码以提供更好的示例。目前,似乎是从网络驱动器读取文件

的问题上的问题

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,该方法延续了:

返回一个长的值,表示文件是最后修改的时间,自时期以来以毫秒为单位(00:00:00 GMT,1970年1月1日)或0L(如果文件不存在)或0L。发生错误。

因此,请查看您的URL,然后将您传递给文件的构造函数。

它和此一样简单(注意:我包括日期格式):

String localPDF = "empty28.pdf";
String originDir = "\\smb\ProdName\ShareName\Temp\username\path\to\file\";
File file = new File(originDir + localPDF);   
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(file.lastModified()));

最新更新