Here we're having the "/" character as the very first letter of the file name if the path contains at least one "/". And I don't think this is your intention. What you want may be as below:
```
dir     = path.substring(0, index + 1),
file    = path.substring(index + 1, path.length);

Note that the second argument of substring is exclusive, meaning the result substring does not include the character at that index.