Java Forum / General / October 2007
Unable to create directory
Alan - 08 Oct 2007 19:08 GMT 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();} }
}
Alan - 08 Oct 2007 19:47 GMT 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
Andrew Thompson - 08 Oct 2007 19:52 GMT >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>
 Signature Andrew Thompson http://www.athompson.info/andrew/
Alan - 08 Oct 2007 20:56 GMT Andrew,
What JDK version and operating system were you running this on?
Thanks, Alan
Alan - 08 Oct 2007 21:49 GMT 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
Piotr Kobzda - 08 Oct 2007 22:47 GMT > This is bizarre. The following URLs (converted to directory names > by code above) work, but "www.weather.gov" still does not! As a workaround, try to use "www.nws.noaa.gov" instead. ;-)
piotr
Alan - 09 Oct 2007 02:15 GMT Does anyone else get this same result on Vista?
Alan - 09 Oct 2007 03:08 GMT Does anyone else running Windows Vista have this problem?
Andrew Thompson - 09 Oct 2007 03:41 GMT >> This is bizarre. The following URLs (converted to directory names >> by code above) work, but "www.weather.gov" still does not! > >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.
 Signature Andrew Thompson http://www.athompson.info/andrew/
Roedy Green - 09 Oct 2007 04:08 GMT > 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 . . .
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Alan - 09 Oct 2007 17:56 GMT 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
Andrew Thompson - 09 Oct 2007 18:22 GMT (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>
 Signature Andrew Thompson http://www.athompson.info/andrew/
Alan - 09 Oct 2007 20:20 GMT This one sounds even closer, although not exactly the same:
File operation report wrong success flag when run with UAC turned on (win)
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6572244
Alan - 10 Oct 2007 19:28 GMT 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
Lew - 11 Oct 2007 07:15 GMT > 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.
 Signature Lew
Roedy Green - 11 Oct 2007 21:37 GMT > 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.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Roedy Green - 09 Oct 2007 03:55 GMT > 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.
 Signature Roedy Green Canadian Mind Products The Java Glossary http://mindprod.com
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|