簡體   English   中英

上次修改的Java文件返回0

[英]Java file last modified returns 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,我在做什么錯? 該文件沒有任何類型的鎖,它是常規的(雖然是空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 ,方法lastModified:

返回一個長值,該值表示該文件的上一次修改時間,以該時間段(自1970年1月1日格林尼治標准時間00:00:00)開始的毫秒數為單位,如果文件不存在或發生I / O錯誤,則返回0L。

因此,檢查您傳遞給File的構造函數的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()));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM