我正在尝试编写自定义脚本选择器,为此我需要读取每个文件的内容。
有没有办法使用java而不是javascript作为scriptselector的语言?如果没有,读取File对象有没有办法,听起来很傻?
<scriptselector language="javascript">
f = self.getFile();
println(f);
//how to read the File?
self.setSelected(true);
</scriptselector>
答案 0 :(得分:5)
这是怎么回事。有点像:
<scriptselector language="javascript">
importPackage(java.io);
importPackage(org.apache.tools.ant.util);
fileUtils = FileUtils.getFileUtils();
f = self.getFile();
println(f);
if( f.getAbsolutePath().endsWith(".xyz") ){
fis = new FileInputStream(f.getAbsolutePath());
isr = new InputStreamReader(fis);
println('reading it!');
fileContents = fileUtils.readFully(isr);
println(fileContents);
self.setSelected(true);
}
</scriptselector>
答案 1 :(得分:4)
您应该能够使用任何BSF-supported language,其中包括BeanShell(Java)。例如:
<script language="beanshell">
String file = "foo.txt";
InputStream is = null;
try {
is = new FileInputStream(file);
// read from stream as required
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) { /* ignore */ }
}
}
</script>