Java NIO file path issue

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

    Depending on how are you going to use the Path object, you may be able to avoid using Path at all:

    // works with normal files but on a deployed JAR gives "java.nio.file.InvalidPathException: Illegal char <:> "
    URL urlIcon = MyGui.class.getResource("myIcon.png");
    Path pathIcon = new File(urlIcon.getPath()).toPath();
    byte bytesIcon[] = Files.readAllBytes(pathIcon);
    
    
    // works with normal files and with files inside JAR:
    InputStream in = MyGui.class.getClassLoader().getResourceAsStream("myIcon.png");
    byte bytesIcon[] = new byte[5000];
    in.read(bytesIcon);
    

提交回复
热议问题