0

嗨,我正在将路径字符串从 Scala 传递给 Groovy 脚本,如下所示,但是当字符串包含 2 个字节字符时,格式会出现乱码。你如何正确地将字符串传递给 groovy 脚本?

var gse = new GroovyScriptEngine()
var scriptClass = gse.loadScriptByName("script.groovy") 
var i = scriptClass.newInstance().asInstanceOf[GroovyObject]
i.setProperty("DIR_HERE", new File(".").getAbsolutePath())
 // when the path contains 2 bytes code like "c:/あああああ/bleh", 
 // the passed string will be garbled and will be like "c:/????????????/bleh"
4

1 回答 1

1

如果我写成script.groovy

println DIR_HERE

然后是一个新文件run.groovy

def gse = new GroovyScriptEngine( '.' )
def scriptClass = gse.loadScriptByName("script.groovy") 
def i = scriptClass.newInstance()
i.setProperty("DIR_HERE", 'c:/あああああ/bleh' )
i.run()

然后当我运行时:

groovy run.groovy

从命令行,它打印:

c:/あああああ/bleh
于 2012-04-11T11:52:29.017 回答