Reputation: 77
I have written a short program that will find a file I have made and print some of its details. It executes all right, but it cannot detect the file size or if it is hidden or not. E.G.
file path: C:\temp\filetext.txt last modified: 0 file size: 0 Is file hidden?false
The file does exist in the temp folder on C. I'm not really sure what the problem is
public void Q1()
{
String fileName = "filetext.txt";
getFileDetails(fileName);
}
public void getFileDetails(String fileName)
{
String dirName = "C:/temp/";
File productsFile = new File(dirName + fileName);
long size = productsFile.length();
System.out.println("file path: " + productsFile.getAbsolutePath() + " last modified: " + productsFile.lastModified() + " file size: " + productsFile.length() + " Is file hidden?" + productsFile.isHidden());
}
Upvotes: 0
Views: 94
Reputation: 1460
File does not need a physical file to work with. Therefore your File object can exist even if the physical file it is supposed to represent does not exist/cannot be found. Check the JavaDoc for length()
and lastModified()
, they both return 0L
in case for example the file does not exist. So make sure your File objects is linked to an existing file on your file system by calling file.exists()
before calling the other methods.
Upvotes: 2