【问题标题】:Write "ANSI" encoded file to disk将“ANSI”编码文件写入磁盘
【发布时间】:2019-07-10 08:57:43
【问题描述】:

我应该使用 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 第二行中的字符应该是: äüöß

【问题讨论】:

  • 您的代码一切正常。 ansii 文件没有任何标记来检测编码,因此您应该手动进行。在记事本++中是encodong - charsets - central european - 1250

标签: groovy encoding cp1252


【解决方案1】:

我想我可能偶然发现了 notepad++ 中的一个错误。 当将我的测试字符串更改为实际数据时,notepad++ 确实开始将特殊字符解释为例外。

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多