Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File.length() always returns 0

807588May 20 2009 — edited May 20 2009
Hello,
I'm trying to check if a file exists and do an action depending on whether the file exists or not. My first plan of approach was simply to use the File.exists() method. However, this always returned true because in a declaration of a File object, if the path representing the File doesn't exist, it is automatically created. So something like File f = new File("saved.file") creates a new file called saved.file. The next thing I tried was to check the length of the file. I had a simple if statement:
File f = new File(saveFile);
if(f.length() == 0L
{
    //tell user that save file doesn't exist
}
else
{
    //process the file
}
However, everytime the code ran the save file was set to 0 bytes and the then part of the if statement executed. I even tried using the FileSystemView (instead of the File f = new File() declaration earlier) to get a list of files and seeing if the save file was one of the files listed, but it always showed up in the list of files, even if I manually deleted the file before running the code.

Does anyone know what's going on or how to fix this? If it helps, I am using NetBeans 6.5 on Windows, and have run the application as a stand-alone jar and inside NetBeans (and have done a "Clean and Build" before new runs).

Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 17 2009
Added on May 20 2009
8 comments
1,738 views