To best understand what is possibly happening I'd recommend debugging through the code. Below I'll explain what could be the issue based on my understanding of the source code.
First of all there is different implementations of Path
and as you mentioned you're working on Windows so I had a look at the source code for WindowsPath
which I found here.
So the method Path.toFile()
is pretty simple. It is:
public final File toFile() {
return new File(this.toString());
}
this
is referring to the instance of the Path implementation and in the case of Windows it's an implementation of WindowsPath
.
Looking at the WindowsPath
class we see toString()
implemented as follows:
@Override
public String toString() {
return path;
}
Now looking at how this path
variable is built. The WindowsPath
class invokes the WindowsPathParser
class which builds the path. The source for WindowsPathParser
can be found here.
The parse
method in WindowsPathParser
is where you'll need to debug to find out exactly what is happening. Depending on your initial path
that you pass as a method parameter this method parses that as a different WindowsPathType
e.g. ABSOLUTE, DIRECTORY_RELATIVE.
The below code shows how the initial path
input can change the type of WindowsPathType
Code
private static final String OUTPUT = "Path to [%s] is [%s]";
public static void main(String args[]) throws NoSuchFieldException, IllegalAccessException {
printPathInformation("c:\\dev\\code");
printPathInformation("\\c\\dev\\code");
}
private static void printPathInformation(String path) throws NoSuchFieldException, IllegalAccessException {
Path windowsPath = Paths.get(path);
Object type = getWindowsPathType(windowsPath);
System.out.println(String.format(OUTPUT,path, type));
}
private static Object getWindowsPathType(Path path) throws NoSuchFieldException, IllegalAccessException {
Field type = path.getClass().getDeclaredField("type");
type.setAccessible(true);
return type.get(path);
}
Output
Path to [c:\dev\code] is [ABSOLUTE]
Path to [\c\dev\code] is [DIRECTORY_RELATIVE]