3

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.

4
  • 2
    boolean fileEx = localFile.isFile(); //returns false? That "returns false" seems important... Commented Sep 26, 2017 at 10:15
  • It does, to me it looks like it doesnt even register the file, however that makes little to no sense to me. Will test on local file. Commented Sep 26, 2017 at 10:18
  • if it returns 0, that means the file doesn't exist. docs.oracle.com/javase/7/docs/api/java/io/…
    – Stultuske
    Commented Sep 26, 2017 at 10:23
  • I tested the implementation with local file and it worked, updated question. Commented Sep 26, 2017 at 10:27

2 Answers 2

5

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.

2

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()));
6
  • I can confirm that this works, however not for file saved on network shared drive. Commented Sep 26, 2017 at 10:34
  • @LoneWanderer It does work for the network; maybe try editing your path? - I've slightly updated my answer (note the four slashes)
    – achAmháin
    Commented Sep 26, 2017 at 10:50
  • I bring news! I tried this with both methods. With original implementation, is still doesnt work, however when I check if the path is absolute, it returns true . This means that the path is correct, however it still doesnt recognize the file. When I used second implementation (hence EDIT 3) it i get and exception the network path was not found with this path \\smb\ProdName\ShareName\Temp\username\path\to\file\empty28.pdf Commented Sep 26, 2017 at 11:20
  • Not sure my man, I literally just created that exact path (with changing smb to one already on my system) and it executes as expected; if you paste the path into the run box (assuming Windows), does it open the folder? (\\smb\ProdName\ShareName\Temp\username\path\to\file). If so, did you add the extra slashes, i.e. double each one - IDEs may do this automatically.
    – achAmháin
    Commented Sep 26, 2017 at 11:29
  • Interestingly, no, it does not. I would assume thats my problem. I tried to use my mapped drive (since I have this share mapped to Z:) and it worked like a charm, however this is likely due to the fact, that windows treats it as logical drive. Is there a way to get the network address of that particular location? Commented Sep 26, 2017 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.