java文件读取文件:java.nio.file.InvalidPathException: Illegal char <:> at index 2

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”)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值