Exception in thread "main" java.nio.file.InvalidPathException: Illegal char < > at index 13: d:\logs\b.txt
at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153)
at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77)
at sun.nio.fs.WindowsPath.parse(WindowsPath.java:94)
at sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:255)
at java.nio.file.Paths.get(Paths.java:84)
at SocketServer.main(SocketServer.java:29)
*****
开始网上百度,说jdk版本问题,我把自己用了许久的1.8_162,换成了1.8_211,但是**仍然报错
(虽然jdk1.12都已经横空出世,但是我还在用1.8,跟不上,真的更新太快了。。。)
也有人说是特殊字符,但说的不太详细,所以自己解释一下
*****
WindowsPathParser.java:182 //代码报错处,代码中,抛出异常的原因,点击进入,就可以看到
if (isInvalidPathChar(var6)) { //这个判定说明你的path的路径中有以下非法字符,再次进入方法看到
throw new InvalidPathException(var1, "Illegal char <" + var6 + ">", var2);
}
private static final boolean isInvalidPathChar(char var0) {
return var0 < ' ' || "<>:\"|?*".indexOf(var0) != -1; //这里是具体的非法字符
//非法字符(空格键对应ascii表的32),这说明你path路径中,有ascii表32位以前的字符(一般为不可见字符,或者是\r \t等等 )
或者有"<>:\"|?*"这里面的几个符号
}
ASCII表参考查询网址百度百科
https://baike.baidu.com/pic/ASCII/309296/0/c2fdfc039245d688c56332adacc27d1ed21b2451?fr=lemma&ct=single#aid=0&pic=c2fdfc039245d688c56332adacc27d1ed21b2451
//**下面举两个例子,都会报同样的错误
//例子1.含有特殊字符*号情况
Path path1 = Paths.get("d:\\logs\\a*.txt");
//==================================================================================
//例子2.含有不可见字符情况
FileInputStream fileOutputStream = new FileInputStream("d:\\logs\\a.txt");
//d:\logs\a.txt文件中的内容就是d:\logs\a.txt
//这里创建了一个30长度的字节数组
byte[] bytes = new byte[30];
//将他d:\logs\a.txt写入这个字节数组,因为d:\logs\a.txt内容长度不够30,所以bytes中的后面会出现NUL这样的不可见字符
fileOutputStream.read(bytes);
String fileName = new String(bytes);
//但是转成字符串输出他就是这样的d:\logs\a.txt,看不出任何的毛病
System.out.println(fileName);//看到的结果就是d:\logs\a.txt
//但是你在这一步操作时,就会报错
//java.nio.file.InvalidPathException: Illegal char < > at index 13: d:\logs\a.txt
//因为fileName中含有不可见字符
Path path2 = Paths.get(fileName);
//*******************报以上错误的还有很多情况,在这里不做具体分析了
欢迎大家点赞,关注,转发......,快点扶我上位。