Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Date on File? 1

Status
Not open for further replies.

TGJ

Programmer
Jan 31, 2005
64
CA
Hi, I am wondering how I can read the "Date Modified" property of a file that my Java program is accessing.

Thanks,
Terry
 
After creating and writing to a file, how can I reference that file later (after it is closed) to see the "last date modified". ie how can I open the file again without altering it in order to call the lastmodified() function?

Thanks,
Terry
 
Calling File.lastModified() does not alter the "modified" date - so just call :

Date d = new Date(new File(myFileName).lastModiied());

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
This isn't supposed to modify the modification date.
Code:
java.io.File f = new java.io.File("path);
System.out.println(f.lastModified());

Cheers,
Dian
 
Thanks for the replies, I think both should work but I am having some troubles.

The lastmodified() function returns a "long" value.
This is what my code now looks like:


File test = new File ("C:\\dipdata\\BatchImport\\BatchImport1");

long d = test.lastModified();


"d" gets a value of 0. I am not sure what is wrong with the code, the BatchImport1 file does exist, any ideas??

Thanks.
 
I expect your file does not exist - test for it first and handle the error :

Code:
File test = new File ("C:\\dipdata\\BatchImport\\BatchImport1");

if (!test.exists()) {
  // handle error
}
long d = test.lastModified();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Hmmm... it appears to the program that it doesn't exist, yet I can see it in that particular directory. Any idea why it can not see the file?
 
Well, I can pretty much assure you that if the code says your file does not exist ... then it does not exist. You must have made a typo ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Man I am dumb, I forgot the .txt, thanks for the help guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top