【问题标题】:How to print special characters in groovy如何在groovy中打印特殊字符
【发布时间】:2013-01-10 21:41:03
【问题描述】:

我想在 groovy 中打印 √ (\u221A) http://www.fileformat.info/info/unicode/char/221a/index.htm 字符

file = new File("log.txt")
if(!file.exists()){
    file.createNewFile()
}

file.withWriter('UTF-8'){
    file.write("\u221A √")
}

我在十六进制编辑器中得到 3F 20 E2 3F 9A

我不知道为什么会这样。在第二种情况下,它看起来类似于 0xE2 0x88 0x9A,这是该字符的正确 UTF-8。

我尝试使用选项 -c UTF-8 运行 groovy,但得到 3F 20 3F

【问题讨论】:

  • 如果我运行该脚本,并以 UTF-8 格式保存,输出是字节值 0xe2 0x88 0x9a 0x20 0xe2 0x88 0x9a 符合预期。你的源文件的编码是什么?
  • 我认为文件是 UTF-8 格式的源文件的十六进制代码是这样的:file.append("\u221A â.š") / 66 69 6C 65 2E 61 70 70 65 6E 64 28 22 5C 75 32 32 31 41 20 E2 88 9A 22 29
  • 我将函数从 file.write 更改为 file.append 但其他一切都保持不变

标签: java groovy


【解决方案1】:

原来我需要

JAVA_TOOL_OPTIONS -Dfile.encoding=UTF8 

在我的 Windows 环境变量中。

【讨论】:

    【解决方案2】:

    对于使用 Groovy 脚本处理此错误并使用其他控制台(例如 Git Bash)的用户,请将变量名称 JAVA_TOOL_OPTIONS 更改为 JAVA_OPTS

    export JAVA_OPTS=-Dfile.encoding=UTF-8
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      相关资源
      最近更新 更多