簡體   English   中英

如何在Groovy腳本引擎中不亂碼的情況下傳遞2個字節的字符串?

[英]How to pass 2 bytes string without being garbled in Groovy Script Engine?

嗨,我正在將路徑字符串從Scala傳遞到Groovy Script,如下所示,但是當字符串包含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"

如果我將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM