Consider a method starting like this:
private void writeFile(String dir, String name) {
Path path = Paths.get(dir, name);
// create & write to file, etc...
Now the caller of this method might want to create the file in a specified directory, such as one of the following:
The problem is that the last one (i.e. Paths.get("\\", "x.txt")
) gives the following error:
Exception in thread "main" java.nio.file.InvalidPathException: UNC path is missing sharename: \\x.txt
Would this be considered a bug? If not, how would you create a file in the root of the current drive in Windows?
Yeah, I'd consider that a bug.
If you look at the code for WindowsFileSystem.getPath(String paramString, String... paramVarArgs)
, you'll find this code (jdk1.8.0_51, source of WindowsFileSystem not included, so this is from disassembler):
public final Path getPath(String paramString, String... paramVarArgs)
{
String str1;
if (paramVarArgs.length == 0) {
str1 = paramString;
} else {
StringBuilder localStringBuilder = new StringBuilder();
localStringBuilder.append(paramString);
for (String str2 : paramVarArgs) {
if (str2.length() > 0) {
if (localStringBuilder.length() > 0)
localStringBuilder.append('\\');
localStringBuilder.append(str2);
}
}
str1 = localStringBuilder.toString();
}
return WindowsPath.parse(this, str1);
}
The code adds \
between elements, even if preceding element already ends with a \
, relying on a\\b
to be compacted to a\b
in the WindowsPath.parse()
method.
So, get("\\", "x.txt")
becomes \\x.txt
, and two \
at the beginning is a UNC path, so parse()
fails.
Suggestion, if you want to file a bug. Change code to:
if (str2.length() > 0) {
if (localStringBuilder.length() > 0) {
char c = localStringBuilder.charAt(localStringBuilder.length() - 1);
if (c != '\\' && c != '/')
localStringBuilder.append('\\');
}
localStringBuilder.append(str2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With