[英]How to load the file from the inputstream?
I have the following code: 我有以下代码:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.groovy");
I need to pass this resource to the method called parse
which takes File
as param, so doing so fails: 我需要将此资源传递给名为parse
的方法,该方法将File
作为param,因此这样做会失败:
new GroovyShell().parse(new FileInputStream(in));
How I can convert FileInputStream
to File
in java? 如何在java中将FileInputStream
转换为File
?
您实际上可以使用: GroovyShell #parse(Reader)方法:
Script result = new GroovyShell().parse(new InputStreamReader(in));
use this : 用这个 :
try {
URL url = this.getClass().getClassLoader().getResource("test.groovy");
File file = new File(url.toURI());
new Shell().parse(file);
} catch(...){...}
if you know exact path to "test.groovy", do this: 如果您知道“test.groovy”的确切路径,请执行以下操作:
new Shell().parse(new File("path/to/test.groovy"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.