简体   繁体   English

使用Java编写UTF-8文件

[英]Using java to write a UTF-8 File

I am trying to write a java utility that writes out an UTF-8 file with just the characters I explicity write to the file. 我正在尝试编写一个Java实用程序,该程序只用我明确写入该文件的字符写出一个UTF-8文件。 I wrote the following code to do the trick. 我编写了以下代码来解决问题。

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;


public class FileGenerator {

    public static void main(String[] args) {
        try {

            char content = 0xb5;

            String filename = "SPTestOutputFile.txt";

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(filename), "UTF-8"));

            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

I also pass -Dfile.encoding=UTF-8 as a VM argument. 我还将-Dfile.encoding = UTF-8作为VM参数传递。

The character that I am trying to write does get written to the file but I also get a  before it so when I try to write out µ I actually get µ. 我尝试写入的字符确实写入了文件,但在此之前我也得到了Â,因此当我尝试写出µ时,实际上得到了µ。 Does anyone know how to correct this so that I always just get just µ? 有谁知道如何纠正这个问题,以便我总是只得到µ?

Thanks 谢谢

The implementation works just fine: the UTF-8 representation for µ is c2 b5 . 该实现工作得很好: µ的UTF-8表示为c2 b5 That is exactly what is written to the file. 这正是写入文件的内容。

Check UTF-8 table here . 在此处检查UTF-8表。

十六进制编辑器中的文件

Your txt file contains two "symbols": 您的txt文件包含两个“符号”:

  1. BOM ( Byte order mark ) BOM( 字节顺序标记
  2. µ μ

If your application (some reader) recognizes encoding correctly, you see only µ . 如果您的应用程序(某些阅读器)能够正确识别编码,则只会看到µ In other cases application interprets BOM as another symbol and you can see µ or something else. 在其他情况下,应用程序将BOM解释为另一个符号,您可以看到µ或其他符号。

So your text file is OK. 这样您的文本文件就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM