Can't access file in .jar via URL

前端 未结 1 363
花落未央
花落未央 2021-01-28 20:50

I need to access a file inside the currently executed .jar using a URL.

URL url = BlockConverter.class.getResource(\"/test.txt\");
System.out.println(url.toStrin         


        
相关标签:
1条回答
  • 2021-01-28 21:11

    I would open it as a stream directly e.g.

    InputStream  is = BlockConverter.class.getResourceAsStream("/test.txt");
    

    The above method is the way I normally access resources within a jar (it will open the resource regardless of it being packaged within a jar, or simply as an unpackaged deployment, note)

    0 讨论(0)
提交回复
热议问题