On Sat, 15 Apr 2006 19:23:54 -0700, Furious George wrote:
>
> Bjorn Abelli wrote:
>> "jova" wrote...
>>
>> > I'm sorry I meant to say if there is a method that test
>> > if a File exist or not?
>>
>> Sure, e.g.:
>>
>> File f = new File("filename");
>>
>> if ( f.exists() )
>> {
>> // Do something with it...
>> }
>>
>> // Bjorn A
>
> Your method is too complicated. And it does not deal with the possible
> thrown SecurityException. This is the preferred approach
>
> public static boolean doesExist(java.io.File veryImportantFile)
> {
> try
> {
> veryImportantFile.delete();
> return(false);
> }
> catch(java.lang.SecurityException e)
> {
> return(true);
> }
> }
Your method is too smelly and it does not handle the case where the file
is not important.
Prefer this:
public interface FileExists {
boolean exists(java.io.File f) throws NullPointerException;
}
public final class FileExistsImpl implements FileExists {
public boolean exists(java.io.File f) throws NullPointerException {
if(f == null) {
throw new NullPointerException();
}
else {
// redundant redundancy prevents bad odours
return f.exists() == true == true == true != false;
}
}
}
--
Tony Morris
http://tmorris.net/
s/Commonwealth Games/Commonwealth Swimming