Re: Unable to create directory
Alan wrote:
I`m trying to create a directory for each URL string read in from
a file. However, one of the directories cannot be created, but the
other can. Neither exist at the start. No exception occurs.
Am I missing something obvious? (I have not used mkdir before.)
Your example failed to fail for me here. Both
directories were created, if I deleted them, I
could create them again.
OTOH - I could not resist tweaking your code..
It uses mkdirs() rather than mkdir(). (The 's' is
an important distinction.)
<sscce>
import java.io.*;
import java.lang.*;
public class DirectoryTree
{
public static void main ( String[] args )
{
try
{
BufferedReader infile =
new BufferedReader(new FileReader("URLs.txt"));
String aURL, directory;
while ((aURL = infile.readLine()) != null)
{
System.out.println(aURL);
directory = (aURL.replace("http:",""))
.replace("/","")
.replace(".","/");
String[] parts = directory.split("/");
File f = new File("cache");
for (int ii=parts.length-1; ii>-1; ii--)
{
f = new File( f, parts[ii] );
}
System.out.println(
"Creating directory " + f + " . . .");
try
{
// important to use makedirs for this variant!
if ((f.mkdirs()) == false)
{
System.out.println(
"Unable to create directory " + directory);
}
}
catch (SecurityException e)
{
e.printStackTrace();
}
}
infile.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
</sscce>
--
Andrew Thompson
http://www.athompson.info/andrew/
Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1