Java NIO file path issue

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

    To make it work on both Windows and Linux\OS X consider doing this:

    String osAppropriatePath = System.getProperty( "os.name" ).contains( "indow" ) ? filePath.substring(1) : filePath;
    

    If you want to worry about performance I'd store System.getProperty( "os.name" ).contains( "indow" ) as a constant like

    private static final boolean IS_WINDOWS = System.getProperty( "os.name" ).contains( "indow" );
    

    and then use:

    String osAppropriatePath = IS_WINDOWS ? filePath.substring(1) : filePath;
    

提交回复
热议问题