My problem is associated with a function to test, whether a path contains characters that are not allowed for a given operating system. So e.g. for Windows this might be characters like '>', '|' or ':' and others (see https://msdn.microsoft.com/en-us/library/aa365247).
The code I use is basically the same as proposed on this webpage: http://eng-przemelek.blogspot.de/2009/07/how-to-create-valid-file-name.html.
private boolean testIfFileNameIsValid(String fileUri) {
boolean invalid = true;
try {
File candidate = new File(fileUri);
// Line in question:
// If it is removed, invalid filenames will not be detected.
candidate.getCanonicalPath();
boolean b = candidate.createNewFile();
if (b) {
candidate.delete();
}
invalid = false;
} catch (IOException ioEx) { }
return invalid;
}
My problem with this code snipped is the line I marked with a comment. When this line is deleted, invalid filenames will not be discovered by the function.
So if the code is used as listed above, for example the following filename would be detected as invalid on a Windows computer:
C:\Users\Me\f:ile.txt
If this line is deleted, the above stated filename is marked as valid.
As this code does not seem to set anything, I am confused why this line has such an influence on the functions result. Could someone explain me this behaviour?