Code cannot find my file

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

Answers


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.


Need Your Help

Where and when to use Lambda?

ruby language-agnostic lambda

I am trying to understand why do we really need lambda or proc in ruby (or any other language for that matter)?

Cannot match the parameter list for MessageBox::Show

c++ .net c++-cli

I'm attempting to display the Error message denoted by the code given to me by GetLastError() and formatted by FormatMessage() within a MessageBox. (Using C++/CLI)

Oracle Triggers not showing in DBA_SOURCE

oracle triggers

In our application, only about 25% of the database triggers show up in DBA_SOURCE. I know I can force the others to show up if I make an actual modification (like adding and removing a space) and ...