Unable to create directory

A

Alan

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.)

I am running on the Windows Vista OS. Thanks, Alan

Output:

http://www.weather.gov/
Creating directory www.weather.gov . . .
Unable to create directory www.weather.gov
http://www.cnn.com/
Creating directory www.cnn.com . . .

Code:

import java.io.*;
import java.lang.*;

public class create_directory
{
public static void main ( String[] args ) throws IOException
{
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("/","");
System.out.println("Creating directory " + directory + " . . .");
try
{
if ((new File(directory).mkdir()) == false)
System.out.println("Unable to create directory " + directory);
}
catch (SecurityException e) {e.printStackTrace();}
}
infile.close();
}
catch (IOException e) {e.printStackTrace();}
}

}
 
A

Alan

Strange . . . This happens under Windows Vista and Java SE JDK 1.6,
but it does not happen under Windows 98SE and JDK 1.5.

Alan
 
A

Andrew Thompson

Alan said:
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
 
A

Alan

Andrew,

What JDK version and operating system were you running this on?

Thanks, Alan
 
A

Alan

This is bizarre. The following URLs (converted to directory names
by code above) work, but "www.weather.gov" still does not! This
happens under Windows Vista, with either Java SE JDK 1.5 or 1.6. It
does NOT happen under Windows 98SE with JDK 1.5. WTF????

Anybody have any ideas what is going on? Thanks, Alan

http://www.brother.com
http://www.cnn.com
http://www.weathe.gov
http://www.weath.gov
http://www.weat.gov
http://www.wea.gov
http://www.we.gov
http://www.weather1.gov
http://www.weather12.gov
http://www.weather123.gov
http://www.weather1234.gov
http://www.weather12345.gov
1234567890
12345678901
123456789012
1234567890123
12345678901234
123456789012345
1234567890123456
12345678901234567
123456789012345678
1234567890123456789
12345678901234567890
 
A

Andrew Thompson

Piotr said:
As a workaround, try to use "www.nws.noaa.gov" instead. ;-)

I think that is a good suggestion. Try .gov1 as well.

To answer an earlier question, I am using 1.6 on XP Pro.

As far as this problem goes, I have a sneaking suspicion
it /might/ be tied up with a recent bug that cropped up re
IE/Vista. That problem, though, affected IE/Vista & trusted
JWS & applets, Vista being the only one of which seems
immediately evident here.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
R

Roedy Green

Am I missing something obvious?

The usual problems are:

1. the directory already exists.

2. there is a file already by that name.

3. you are trying to create a directory when the parent does not yet
exist. Use mkdirs with an s to create parents automatically.

Go a have a peek with explorer to see what is going on.
 
R

Roedy Green

This is bizarre. The following URLs (converted to directory names
by code above) work, but "www.weather.gov" still does not! This
happens under Windows Vista, with either Java SE JDK 1.5 or 1.6. It
does NOT happen under Windows 98SE with JDK 1.5. WTF????

I ran Andrew's code with your list of URLS and all worked fine as
best as I could tell on Vista home premium .

I made one small modification. " == false" sets my teeth on edge, and
corrected a typo in the comment.

// important to use mkdirs for this variant!
if ( !f.mkdirs() )
{
System.out.println(
"Unable to create directory " +
directory);
}




http://www.brother.com
Creating directory cache\com\brother\www . . .
http://www.cnn.com
Creating directory cache\com\cnn\www . . .
http://www.weathe.gov
Creating directory cache\gov\weathe\www . . .
http://www.weath.gov
Creating directory cache\gov\weath\www . . .
http://www.weat.gov
Creating directory cache\gov\weat\www . . .
http://www.wea.gov
Creating directory cache\gov\wea\www . . .
http://www.we.gov
Creating directory cache\gov\we\www . . .
http://www.weather1.gov
Creating directory cache\gov\weather1\www . . .
http://www.weather12.gov
Creating directory cache\gov\weather12\www . . .
http://www.weather123.gov
Creating directory cache\gov\weather123\www . . .
http://www.weather1234.gov
Creating directory cache\gov\weather1234\www . . .
http://www.weather12345.gov
Creating directory cache\gov\weather12345\www . . .
1234567890
Creating directory cache\1234567890 . . .
12345678901
Creating directory cache\12345678901 . . .
123456789012
Creating directory cache\123456789012 . . .
1234567890123
Creating directory cache\1234567890123 . . .
12345678901234
Creating directory cache\12345678901234 . . .
123456789012345
Creating directory cache\123456789012345 . . .
1234567890123456
Creating directory cache\1234567890123456 . . .
12345678901234567
Creating directory cache\12345678901234567 . . .
123456789012345678
Creating directory cache\123456789012345678 . . .
1234567890123456789
Creating directory cache\1234567890123456789 . . .
12345678901234567890
Creating directory cache\12345678901234567890 . . .
 
A

Alan

As far as this problem goes, I have a sneaking suspicion
it /might/ be tied up with a recent bug that cropped up re
IE/Vista. That problem, though, affected IE/Vista & trusted
JWS & applets, Vista being the only one of which seems
immediately evident here.

Andrew,

Do you have a web link that describes this problem? With Vista,
it seems to be: "So many problems, so little time."

Thanks for mentioning this. I have not found a reference to one
like this.

Roedy,
I watched with Explorer, but I saw nothing unusual there.

There are workarounds, but it is just perplexing and
frustrating.
Thanks, Alan
 
A

Andrew Thompson

Alan wrote:

(A.T.)
As far as this problem goes, I have a sneaking suspicion
it /might/ be tied up with a recent bug that cropped up re
IE/Vista. That problem, though, affected IE/Vista & trusted
JWS & applets, Vista being the only one of which seems
immediately evident here.
Alan
Do you have a web link that describes this problem?

OK, just quickly, it was something to do with
Vista and files, ..
<http://www.google.com/search?as_q=vista+file&as_sitesearch=bugs.sun.com>

This is probably the one.
"Bug ID: 6548078 The Java Security Model is broken on Windows Vista ..."
<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548078>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
A

Alan

Well, I found the problem. Windows Vista does not like it when I
try to make a directory with the same name (without extension) as a
different type file (e.g., "www.weather.gov" (HTML file) or
"www.weather.gov (Word file)). Windows 98 does not mind. Vista
handles extensions differently.


I found that if I first check to see if the directory already
exists, and then create it if not, everything works fine! So,
something screwy in JDK on Vista must be happening when I try
"mkdirs()" without checking the existence first. Odd. . . .

Alan
 
L

Lew

Alan said:
Well, I found the problem. Windows Vista does not like it when I
try to make a directory with the same name (without extension) as a
different type file (e.g., "www.weather.gov" (HTML file) or
"www.weather.gov (Word file)). Windows 98 does not mind. Vista
handles extensions differently.

Comparing anything to how Win 98 works is pretty amusing.
 
R

Roedy Green

I found that if I first check to see if the directory already
exists, and then create it if not, everything works fine! So,
something screwy in JDK on Vista must be happening when I try
"mkdirs()" without checking the existence first. Odd. . . .

mkdirs takes a while to do its job. If you check existence afterward,
you might not see its result. That may have some bearing on this
strange behaviour.
 

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

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top