Java NIO file path issue

前端 未结 8 2053
臣服心动
臣服心动 2020-12-01 11:36

I used the following code to get the path

Path errorFilePath = FileSystems.getDefault().getPath(errorFile);

When I try to move

8条回答
  •  悲&欢浪女
    2020-12-01 12:11

    You need to convert the found resource to URI. It works on all platforms and protects you from possible errors with paths. You must not worry about how full path looks like, whether it starts with '\' or other symbols. If you think about such details - you do something wrong.

    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    String platformIndependentPath = Paths.get(classloader.getResource(errorFile).toURI()).toString();
    

提交回复
热议问题