简体   繁体   中英

Java file last modified returns 0

I have the following code to check the last modification on a file saved on network drive.

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;

}

The parameter given to constructor of a file is following:

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

However, the lastModified() method returns 0 for some reason, what am I doing wrong? The file has no lock of any kind, it is regular (altough empty PDF).

EDIT1: I tested the method on local file, the path was:

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

And the value returned was correct, my suspicion was that method was not allowed to execute on a given file since it's on network drive. However, this check happens on one thread that is already authorized. No clue where the error is.

EDIT2: I updated code to provide better examples. Right now, it seems the problem is with reading file from network drive

EDIT3 I Tried using different method. Imported:

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

and added code:

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

with same result, again, local drive works and remote is not working.

According to the Javadocs the method 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.

So, check out your URL you passes to the File's constructor.

It's as simple as this (note: I included a date format):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM