Creating files in directories that don't exist : problems

L

Lee

Hi there,

I wonder if anyone can offer me some advice. I'm trying to pass a
filename (full path and filename e.g. C:/usr/local/myfile.txt) to a
method, and have the file created. Before doing so, I must make sure
that the directories exist!

I do the following

---- passing in file='C:/usr/local/myfile.txt'--
String absolutePath = file.getPath();
File path = null;
path = new File( absolutePath);

if ( !path.exists()){
System.out.println("creating " + path);
boolean success = path.mkdirs();
<snip>
-------------------------------------------------


But that creates a directory called myfile.txt in /usr/local! Can anyone
tell me if there's a way to make it know that myfile.txt is a file, not
a directory?

Thanks very much in advance,
Lee
 
T

Tony Morris

path.getParent().mkdirs();

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
A

Andrew Thompson

| Hi there,
|
| I wonder if anyone can offer me some advice. I'm trying to pass
a
| filename (full path and filename e.g. C:/usr/local/myfile.txt)
to a
| method, and have the file created. Before doing so, I must make
sure
| that the directories exist!

File.mkdirs(); // is the method you require

HTH
 
T

Tony Morris

He/she is using that method, however, the File instance represents an actual
file (as opposed to a directory).
It is up to you (the developer) to create the parent directories for it if
you wish to write to the file.
path.getParent().mkdirs();

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top