上次修改的Java文件返回0

时间:2017-09-26 10:11:06

标签: 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)。

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

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

2 个答案:

答案 0 :(得分:3)

根据Javadocs方法lastModified:

  

返回表示文件上次修改时间的long值,以纪元(格林威治标准时间00:00:00,1970年1月1日)为单位,以毫秒为单位,如果文件不存在或者是I / O,则返回0L发生错误。

因此,请查看您传递给File的构造函数的URL。

答案 1 :(得分:2)

这很简单(注意:我包含了日期格式):

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