java.nio.file.InvalidPathException: Illegal char <:> at index 2
springboot项目中,文件拷贝时使用Files.copy(new File(“C:/a/b.txt”).toPath(), “c/a/b.txt”)报错
java.nio.file.InvalidPathException: Illegal char <:> at index 2: C:/a/b.txt
at sun.nio.fs.WindowsPathParser.normalize(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPathParser.parse(Unknown Source)
at sun.nio.fs.WindowsPath.parse(Unknown Source)
at sun.nio.fs.WindowsFileSystem.getPath(Unknown Source)
at java.io.File.toPath(Unknown Source
原因时未找到C:/a/b.txt,因为springboot项目中读取文件C:/a/b.txt变成了“‘项目根目录’/C:/a/b.txt”
解决办法把原来的方法改一下:
Files.copy(new File(“C:/a/b.txt”).toPath(), “c/a/b.txt”)
↓ ↓ ↓
Files.copy(new File(new File(“C:/a/b.txt”).getPath().substring(1)).toPath(), “c/a/b.txt”)