我应该使用 groovy 以 ANSI 格式创建一个导出文件。当我将文件写入磁盘并在“Notepad++”或 nano 中打开时,“äüö”等特殊字符无法正确显示。Notepad++ 将“Windows-1255”显示为编码。
我已经尝试更改编码并另外转换字符串
import java.io.FileWriter
import java.io.BufferedWriter
import java.io.InputStream
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets
String fullPath = "/tmp/jamesTempFiles/ansi.txt"
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullPath), "Cp1252"))
//doesn't work either when i use the following line instead
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fullPath), "windows-1252"))
String input = "test\r\näüöß"
writer.write(input)
//doesn't work either when i use the following line instead
//writer.write(new String(input.getBytes(Charset.forName("utf-8"))))
writer.close()
在记事本++中,文件看起来像这样 https://pasteboard.co/Inj3SL2.png 第二行中的字符应该是:äüöß