public String getQuery(String nameFile, Package pathFile)
{
// How to get on InputStrem nameFile and pathFile
}
我无法通过类加载器实现
String path = getClass().getPackage().getName().replace('.', File.pathSeparatorChar);
String file = path + "file.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
返回= null
该pathSeparatorChar
是:
在Unix和;
Windows上。它与用于/
在所有平台上从ClassLoader加载资源的char无关。
此外,您忘记了路径和文件名之间的分隔符。它应该是
String path = getClass().getPackage().getName().replace('.', '/');
String file = path + "/file.txt";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(file);
或者,更为简单,因为Class
它具有一种可以直接从与类相同的包中加载资源的方法:
InputStream in = this.getClass().getResourceAsStream("file.txt");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句