我有以下代码:
InputStream in = this.getClass().getClassLoader().getResourceAsStream("test.groovy");
我需要将此资源传递给名为parse
的方法,该方法将File
作为参数,因此这样做会失败:
new GroovyShell().parse(new FileInputStream(in));
如何在java中将FileInputStream
转换为File
?
答案 0 :(得分:2)
您实际上可以使用:GroovyShell#parse(Reader)方法:
Script result = new GroovyShell().parse(new InputStreamReader(in));
答案 1 :(得分:1)
使用它:
try {
URL url = this.getClass().getClassLoader().getResource("test.groovy");
File file = new File(url.toURI());
new Shell().parse(file);
} catch(...){...}
如果您知道“test.groovy”的确切路径,请执行以下操作:
new Shell().parse(new File("path/to/test.groovy"));