File is a directory?

Hi,

I've the following function:

staticpublicvoid setFileContents(File aFile, String aContents)

throws FileNotFoundException, IOException{

if (!aFile.isFile()){

thrownew IllegalArgumentException("Should not be a directory: " + aFile);

}

[...]

and I call it in this way:

File fn =new File("foo.txt");

[...]

setContents(fn,"Hi there");

foo.txt does not exists. Why do I receive the IllegalArgumentException Should not be a directory?

Is it because, since file does not exist, it's not a file? I.e., should it enclose within a

if(aFile.exists(){

if (!aFile.isFile()){

thrownew IllegalArgumentException("Should not be a directory: " + aFile);

}

}

Thanks.

[1465 byte] By [Sandman42a] at [2007-11-27 5:42:04]
# 1
Err... just because a File.isFile() returns false does not make it a directory!
sabre150a at 2007-7-12 15:20:15 > top of Java-index,Java Essentials,Java Programming...
# 2
Yes of course. I've taken the snippet from somewhere on the net.But, if a file does not exist,File.isFile() returns false, am I correct?
Sandman42a at 2007-7-12 15:20:15 > top of Java-index,Java Essentials,Java Programming...
# 3

> Yes of course. I've taken the snippet from somewhere on the net.

And do you take medical advice from "somewhere on the net"?

Trust Dr API: [url=http://java.sun.com/javase/6/docs/api/java/io/File.html#isDirectory()]http://java.sun.com/javase/6/docs/api/java/io/File.html#isDirectory()[/url]

Hippolytea at 2007-7-12 15:20:15 > top of Java-index,Java Essentials,Java Programming...
# 4
> Trust Dr API:> [url= http://java.sun.com/javase/6/docs/api/java/io/Fil> e.html#isDirectory()] http://java.sun.com/javase/6/docs> /api/java/io/File.html#isDirectory()[/url]That's a gentle way to say RTFM. Appreciated.Thanks and ciao
Sandman42a at 2007-7-12 15:20:15 > top of Java-index,Java Essentials,Java Programming...
# 5
> Yes of course. I've taken the snippet from somewhere> on the net.> > But, if a file does not exist,File.isFile() returns> false, am I correct?Err... of course - but what has that got to do with your problem?
sabre150a at 2007-7-12 15:20:15 > top of Java-index,Java Essentials,Java Programming...