public class CLIENT_PATH { String PATH; Path Path; public void INPUT_PATH(){ out.println("Введите путь"); do { out.print(" "); Scanner in = new Scanner(System.in); PATH = in.next(); Path = Paths.get(PATH); if (Files.exists(Path)) { out.println(" Путь введён\n"); } else out.println(" Ошибка"); } while(Files.notExists(Path)); out.println(PATH); } } 

When entering the Russian characters K (R), P (G), O (J), and M (V) as well as the characters ", No. and possibly others throw an exception. I want to emphasize that when entering characters # or $ and others, the exception does not throw How to handle exceptions?

Enter the path

P

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <? > at index 0:?

Enter the path

m

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <<> at index 0: <

  • What do you mean by "treatment"? What, in fact, is the question? Do you want the paths containing these characters to be checked, or do you want to make no mistake when typing these characters? - Peter Samokhin
  • @peterSamokhin Remove errors. Why a mistake? I can not be in the way of letters to, p, o, m? Why does that handle other characters in output? - aaa
  • Not all characters are allowed, for example / \ : * ? " < > | / \ : * ? " < > | prohibited by Windows itself. Now I tried it: Russian paths are not recognized either, but there are no errors. I am sure this problem is solved. You can try using character codes: \u0000 and so on. - Peter Samokhin
  • @PeterSamokhin With forbidden characters is understandable. In my case, Russian paths are recognized, but I don’t know why when any of the above Russian characters is entered, it throws an exception. As if Windows instead of the entered Russian characters takes them for prohibited. - aaa
  • That is, some Russian characters do not cause an error, and some - cause? - Peter Samokhin

1 answer 1

How to calculate all the "bad" characters, and why they cause an error - I can not say, but you can try the following crutch:

 char a = '\u0045'; // 'E' (буква латинского алфавита) char b = '\u041F'; // 'П' String string = a + ":\\" b; Path path = new Paths.get(string); // Папка `E:\\П` // У меня выполнение проверки выдало `true` без любых ошибок // Папка `П`, естественно, существует на диске `E`. 

enter image description here
Checked for different letters - any Russian characters are not recognized, and even if the folder exists, the result of the check will be false .
But if you try the way I wrote above - everything works.
Character codes can be found here: https://unicode-table.com/ru/
The symbol U+0030 in Java interpreted as '\u0030' , for example.
To handle this error, you need to calculate all the "problem" characters and use String.replace("что меняем", "на что меняем") .
Perhaps a stupid decision, but it definitely works.

  • This is not what is needed, in NetBeans, Cyrillic paths work except for those in question. - aaa
  • @ELe try to calculate, as I said, the problem characters and only do so with them. I tried through the console, and Cyrillic is not perceived there - Peter Samokhin
  • @ I created a jar file and there are no errors when typing into the Cyrillic console. Such exceptions are only in the NetBeans console. - aaa