[英]How to get a java/resources file to InputStream?
我在src / main / resources(/ resources / files / templates)中有一个Excel文件。 我想将其读取到InputStream。 我没有为此找到合适的语法。
String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";
try {
InputStream inputStream = new URL("file:///" + fn).openStream();
上面不起作用。 我认为路径是错误的。 如何为资源中存储的文件提供正确的路径?
这对我有用。
InputStream is = YourClass.class.getClassLoader().getResourceAsStream("relative path to your file starting from resources folder");
如果采用任何方法(静态或非静态):
String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";
InputStream in = Clazz.class.getResourceAsStream(fn);
Clazz
是您所在班级的名称。
如果您使用的是非静态方法:
String fn = "/resources/files/censusTemplates/SimpleStandardTemplate.xlsm";
InputStream in = getClass().getResourceAsStream(fn);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.