您需要将它们视为类路径资源,而不是本地磁盘文件系统路径。当您将文件打包到JAR中并且您也不想依赖于工作目录时,这将行不通。JAR中的文件已经是类路径的一部分。
假设您
foo.txt在package中有一个文件
com.example,则可以
InputStream按以下方式获取其中一个文件
InputStream input = getClass().getResourceAsStream("/com/example/foo.txt");// ...或者当您处于
static上下文中
InputStream input = SomeClass.class.getResourceAsStream("/com/example/foo.txt");// ...或者当您想扫描全局类路径时
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("/com/example/foo.txt");// ...


