0

I am working on a script to create sub-directories with names, that are fetched from a database. I am passing 2 parameters, for creating 2 directories, one inside the other. Code snippet is below.

File files = new File(name1+"\"+name2);
if (!files.exists()) {
    if (files.mkdirs()) {
        System.out.println("sub directories created successfully");
    } else {
        System.out.println("failed to create sub directories");
    }
}

The "name2" parameter is of the format "abc/d/e" and this has to be the name of the directory. Using the above code, it create individual folders as abc,d,e. I am working on java & linux platform

Any solution for this.

Arnab
  • 63
  • 3
  • 14

1 Answers1

2

Linux file system entries (file names, directory names, socket names, etc) cannot contain /. This character is reserved as the path separator. What you want to do is not possible.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • actually you can with `mkdir -p foo/bar`. Not quite sure how that maps to the Java file objects. – max Aug 19 '15 at 18:31
  • 2
    That creates a directory `foo` if it does not exist, with a directory `bar` inside it. It does not create a single directory named `foo/bar`. – chepner Aug 19 '15 at 18:33
  • true, but from his code it looks like he actually wants to create subdirectories. – max Aug 19 '15 at 18:41
  • "The "name2" parameter is of the format "abc/d/e" and this has to be the name of the directory. Using the above code, it create individual folders as abc,d,e." He *wants* a single directory, but he's getting three directories. – chepner Aug 19 '15 at 18:44