If you don't mind false positives for identifying paths, then you really just need to ensure the path doesn't contain a NUL
character; everything else is permitted (in particular, /
is the name-separator character). The better approach would be to resolve the given path using the appropriate file IO function (e.g. File.exists()
, File.getCanonicalFile()
in Java).
Long answer:
This is both operating system and file system dependent. For example, the Wikipedia comparison of file systems notes that besides the limits imposed by the file system,
MS-DOS, Microsoft Windows, and OS/2
disallow the characters \ / : ? * " > < |
and NUL
in file and directory
names across all filesystems. Unices
and Linux disallow the characters /
and NUL
in file and directory names
across all filesystems.
In Windows, the following reserved device names are also not permitted as filenames:
CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5,
COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4,
LPT5, LPT6, LPT7, LPT8, LPT9