The specified document name is not a valid file name. File names should not contain the following characters:
- Asterisk (
*)- Vertical bar (
|)- Colon (
:- Double quotes (
").- Less (
<)- More (
>)- Question mark (
?)- Slash (
/)- I will check backslash (
\) as a directory existence.
Please help to create a regular expression to check the occurrence of the above characters in a string (the path from disk to folder), can anyone have a ready-made solution, such an example? I need to check the correctness of the path entered (Checking the existence of the directory does not fit because it throws an exception if you enter an invalid character).
It is necessary to catch all invalid characters when the user enters the path. For example, if I check the path to the existence of directories, I will get an exception because of an invalid character, so I need to catch all the invalid characters. And it is not clear if I will throw an exception if I enter this path C:\folder\fol:der , for the characters < , > and others I received exceptions. You need to be sure that no matter what path the user enters, the program will not shut down and will catch the error and continue entering the path.
I check the path like this:
Path Path; if (Files.exists(Path)) out.println(" Путь введён\n"); else out.println(" Ошибка"); If the variable contains an invalid character, then an exception is thrown:
Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <? > at index 0:? Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <<> at index 0: <