Java 字符编码写入文本文件

Java Character Encoding Writing to Text File

My Issue is as follows:

写入文本文件时出现字符编码问题。问题是字符未显示预期值。例如,我正在写 ' '(可能是制表符),而 'Â' 是文本文件中显示的内容。

Background information

此数据存储在 MSQL 数据库中。数据库排序规则为 SQL_Latin1_General_CP1_CI_AS,字段为 varchar。我来学习整理和类型确定数据库端使用的字符编码。值存储正确,因此这里没有问题。

我的 Java 应用程序运行查询以从数据库中提取数据,这看起来也不错。我已经调试了代码并看到所有字符串在写入文件之前都有正确的表示。

接下来,我使用 OutputStreamWriter 将文本写入 .TXT 文件,如下所示:

public OfferFileBuilder(String clientAppName, boolean isAppend) throws IOException, URISyntaxException {
        String exportFileLocation = getExportedFileLocation();
        File offerFile = new File(getDatedFileName(exportFileLocation+"/"+clientAppName+"_OFFERRECORDS"));
        bufferedWriter  = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(offerFile, isAppend), "UTF-8"));
    }

现在,一旦我通过 运行 cat 命令在文件上打开 Linux 服务器上的文件,或者使用记事本++打开文件,一些字符显示不正确。

我已经 运行 服务器上的以下命令来查看其编码 locale charmap 打印 UTF-8echo $LANG 打印 en_US.UTF-8,以及echo $LC_CTYPE` 什么都不打印。

Here is what I've attempted so far. I've attempted to change the Character encoding used by the OutputStreamWriter I've tried UTF-8, and CP1252. When switching encoding some characters are fixed when others are then improperly displayed.

My Question is this: Which encoding should my OutputStreamWriter be using? (Bonus Questions) how are we supposed to avoid issues like this from happening. The rule of thumb i was provided was use UTF-8 and you will never run into problems, but this isn't the case for me right now.

服务器上的

运行 file -bi 命令显示文件使用 ascii 而不是 utf8 编码。完全删除文件并重新运行这个过程为我解决了这个问题。