File.mkdirs wont create a directory

S

SlowLearner

I'm using netbeans5.5 jdk 1.5.0_09

Everytime I call mkdir or mkdirs it fails. I have administrator rights
and can create the folder in c++ but not in java. It returns false and
the folder isn't created. The code is

public boolean createDir(){
File fp = new File("C:\\Backups");
return fp.mkdirs();
}

any help would be appreciated
 
A

Andrew Thompson

SlowLearner wrote:
....
Everytime I call mkdir or mkdirs it fails. I have administrator rights
and can create the folder in c++ but not in java. It returns false and
the folder isn't created. The code is

public boolean createDir(){
File fp = new File("C:\\Backups");

The best way to build paths is to use the File
constructor that accepts a parent and child.
File fp = new File("C:");
// correct nomenclature for the drive?
System.out.println( "fp.exists(): " + fp.exists() );
fp = new File(fp,"Backup");
System.out.println( "fp.exists(): " + fp.exists() );
return fp.mkdirs();
}

any help would be appreciated

Andrew T.
 
S

Steve W. Jackson

"Andrew Thompson said:
SlowLearner wrote:
...

The best way to build paths is to use the File
constructor that accepts a parent and child.
File fp = new File("C:");
// correct nomenclature for the drive?
System.out.println( "fp.exists(): " + fp.exists() );
fp = new File(fp,"Backup");
System.out.println( "fp.exists(): " + fp.exists() );


Andrew T.

In Andrew's example, I recommend using isDirectory() rather than
exists(), since the former includes the latter in its checks -- that is,
it returns true if and only if the File object (in this case the root of
drive C:) exists AND is a directory. I didn't test it just now, but I'm
pretty sure that using "C:" will return a File object referring to the
root directory of that drive.

I also suggest, however, breaking the habit of *ever* using those stupid
backslashes. Whatever idiot did that years ago should be strung up.
The fact is that modern Windows versions will handle *real* slashes
properly, for the most part -- and Java most certainly will handle them
fine in pretty much any setting I've dealt with thus far.

= Steve =
 
A

Andrew Thompson

In Andrew's example, I recommend using isDirectory() rather than
exists(), since the former includes the latter in its checks -- that is,
it returns true if and only if the File object (in this case the root of
drive C:) exists AND is a directory.

Good point.
...I didn't test it just now, but I'm
pretty sure that using "C:" will return a File object referring to the
root directory of that drive.

I was actually wondering about the capital letter,
but Win seems disturbingly tolerant of mixed
(and incorrect) case.

(from earlier..)
....
I also suggest, however, breaking the habit of *ever* using those stupid
backslashes. Whatever idiot did that years ago should be strung up.

Hear, hear.
The fact is that modern Windows versions will handle *real* slashes
properly, for the most part -- and Java most certainly will handle them
fine in pretty much any setting I've dealt with thus far.

I take the principle..

- 'When in Java - ask the file class', just in case some
wierd-ass OS is developed, where they decide to use
'@' as the seperator! Otherwise ..
- 'let Ant handle it', and use '/' consistently.

Andrew T.
 
J

John W. Kennedy

Steve said:
The fact is that modern Windows versions will handle *real* slashes
properly, for the most part

Actually, support of slashes goes all the way back to DOS 2.0, when
subdirectories were introduced in the first place.
 
S

SlowLearner

Thanks for all your help. Still don't understand why original code
didn't work but the examples you all gave worked.
 
A

Andrew Thompson

SlowLearner said:
Thanks for all your help. Still don't understand why original code
didn't work but the examples you all gave worked.

hmmm... Win. - did you reboot in the meantime? ;-)

Andrew T.
 
D

Daniel Pitts

Andrew said:
Good point.


I was actually wondering about the capital letter,
but Win seems disturbingly tolerant of mixed
(and incorrect) case.

(from earlier..)

Hear, hear.


I take the principle..

- 'When in Java - ask the file class', just in case some
wierd-ass OS is developed, where they decide to use
'@' as the seperator! Otherwise ..
- 'let Ant handle it', and use '/' consistently.

Andrew T.

Aparently you've never worked on VMS.
Typical VMS paths:
[some]
[some.folder]
[some.other]
[some.other.folder]

I wonder what java does in VMS. Hmm.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top