Java NIO file path issue

前端 未结 8 2052
臣服心动
臣服心动 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:22

    To be sure to get the right path on Windows or Linux on any drive letter, you could do something like this:

    path = path.replaceFirst("^/(.:/)", "$1");
    

    That says: If the beginning of the string is a slash, then a character, then a colon and another slash, replace it with the character, the colon, and the slash (leaving the leading slash off).

    If you're on Linux, you shouldn't end up with a colon in your path, and there won't be a match. If you are on Windows, this should work for any drive letter.

提交回复
热议问题