18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C#で、utf8(BOM無し)のテキストファイルを出力する

Last updated at Posted at 2014-11-05

C# で、utf8 のテキストファイルを出力しようと、何も考えずに以下のようなコードを書くと

SAMPLE1
var utf8_encoding = Encoding.GetEncoding("utf-8");
using (var writer = new StreamWriter("UTF8.TXT", false, utf8_encoding))
{
    writer.WriteLine("あいうえお");
}

ファイル先頭に BOM (Byte Order Mark) が付与されたがっかりすることになる。

BOMなしの utf8 なテキストファイルを出力するには、

SAMPLE2
var utf8_encoding = new System.Text.UTF8Encoding(false);
using (var writer = new StreamWriter("UTF8.TXT", false, utf8_encoding))
{
    writer.WriteLine("あいうえお");
}

とすれば良い。

参考:UTF8Encoding クラス (System.Text)

18
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?