1

I'm running into a very frustrating error on what was supposed to be a very simple script (I know java may not be the best for scripting, but here I am).

Anyway, I'm pulling names of printers from a csv file, and then I'm trying to create a folder named for each printer that I pulled from that csv file. The issue is that I can only write a directory for the printer if I type it in like this:

(new File("c:\\print\\printername").mkdir()

but if I do this:

String whatever = "c:\\print\\printername"
(new File(whatever)).mkdir()

no directory will be created. I'm baffled at what the problem might be. I've restructured my code a few times to try and track down the issue, and tried things like switching between mkdir() and mkdirs(), but still nothing. Here's my code

public static void main(String[] args) throws FileNotFoundException{
    Scanner printers = new Scanner(new File("C:/Users/ransom/Desktop/printers.csv"));
    printers.useDelimiter("\n");
    String printerPath = "";

    //new File("C:\\printer\\ISS114-Xerox4150PS").mkdir();

    while(printers.hasNext()){          
        printerPath = "C:\\printer\\"+printers.next();
        if(!(new File(printerPath)).mkdir()){
            System.out.println(printerPath);
        }
    }

}

The line that is commented out is an example of when the script works, but if it gets to that line in the loop it doesn't create a directory. Any ideas?

SRansom
  • 337
  • 2
  • 3
  • 6

2 Answers2

2

Both the ways you have mentioned above (using the explicit String or the String variable) work the same. The problem you seem to be getting is because of the parent directories not existing.

That is if the directory "C:\\print" doesn't exist then mkdir() will not create the directory "C:\\print\\aSubDir"

Try using mkdirs() which will create all the required parent directories also.

See this example ("C:\\Temp already exists):

public static void main(String[] args) throws FileNotFoundException
{
    String path1 = "C:\\Temp\\print\\1";
    String path2 = "C:\\Temp\\print\\2\\2a";

    System.out.println("Attempt 1: " + new File("C:\\Temp\\print\\1\\").mkdir());
    System.out.println("Attempt 2: " + new File(path1).mkdir());
    System.out.println("Attempt 3: " + new File(path1).mkdirs());
    System.out.println("Attempt 4: " + new File(path2).mkdir());
    System.out.println("Attempt 5: " + new File("C:\\Temp\\print\\2\\2a").mkdir());
    System.out.println("Attempt 6: " + new File("C:\\Temp\\print\\2\\2a").mkdirs());
}

Gives the output:

Attempt 1: false
Attempt 2: false
Attempt 3: true
Attempt 4: false
Attempt 5: false
Attempt 6: true

Edit (Thanks @MadProgrammer)

It could also be due to the fact that the directory already exists as if you run the above example a second time all will return false. So you could also add a check if the directoy exists already before creating using File#exists()

Java Devil
  • 10,629
  • 7
  • 33
  • 48
  • If you read the original post, I mentioned that I tried both mkdir() and mkdirs(). The issue isn't that I am getting mixed true false outputs, it's that when I try to navigate to that directory, there is nothing there. – SRansom Jan 09 '14 at 08:58
  • @user1742733 Sorry missed that small part about trying both methods. Will need some more info then. Is this on windows? Also what is example content of your csv file that you are loading? Also I assume that your code prints out a list of all the directories then as they weren't created? – Java Devil Jan 09 '14 at 10:46
  • The csv has no commas in it(while troubleshooting this error I got rid of the columns I didn't need to simplify the code), so on each new line there is the name of a printer. Some have no special characters at all, some contain numbers, and some contain dashes or spaces. None of them are being created, so yes the output is just spitting out the whole list back at me. However, if I take ANY of the printers it spit out to the output, and use mkdir or mkdirs with the printer name enclosed in quotations, it creates the directory. It's very frustrating. Yes this is on windows. – SRansom Jan 10 '14 at 21:14
1

Try that(notice the slashes):

String whatever = "c:/print/printername"
(new File(whatever)).mkdir()

Tell if it worked. If downvote say why(other viewers).

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    Oddly, on Windows, it will make no difference. Other OS's don't like `\\` and treat it literally, but for some reason, on Windows, the separator is auto-magically adjusted... – MadProgrammer Jan 08 '14 at 00:21
  • 1
    @Yoda I agree completely. I haven't used a backslash in Java for a filename in 17 years regardless of platform. – user207421 Jan 08 '14 at 00:25
  • 1
    @MadProgrammer You have that back to front. `/` is magically adjusted on Windows to `\\`. You therefore never have to use `\\` on any platform. – user207421 Jan 08 '14 at 00:25
  • 1
    @EJP I thought that's what I said. I agree, there should be no need to use `\\\` BUT, in the context of the question, it won't make much difference, it's a nice comment to be sure... – MadProgrammer Jan 08 '14 at 00:27
  • 1
    @MadProgrammer No, you said 'the separator is auto-magically adjusted'. It isn't, unless it was `"\\"` and you are on Windows. – user207421 Jan 08 '14 at 00:52
  • 1
    @EJP It would appear that "\\" is being escaped automatically in my comment, and I did say *"on Windows, the separator is auto-magically adjusted"* and I also said *"Other OS's don't like `\\\` and treat it literally"* - but again, it appears it was escaped automatically... – MadProgrammer Jan 08 '14 at 00:55
  • I had actually tried both, I normally use a forward slash, but tried the double backslash to see if that had anything to do with it. Since both versions (The explicit string and the string object) include the slashes in the same direction, and only once works that doesn't seem to be the issue anyway. – SRansom Jan 09 '14 at 08:56