创建没有 BOM 的文本文件 [英] Create Text File Without BOM

查看:27
本文介绍了创建没有 BOM 的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了,以及特殊字符(如Æ Ø Å)不正确:-/

我被卡住了!

我的目标是使用 UTF-8 作为编码和 8859-1 作为 CharSet 创建一个文件

这是很难完成还是我只是度过了糟糕的一天?

非常感谢所有帮助,谢谢!

解决方案

好吧,它会写 BOM,因为您正在指示它,在

Encoding utf8WithoutBom = new UTF8Encoding(true);

true 表示应该发出 BOM,使用

Encoding utf8WithoutBom = new UTF8Encoding(false);

不写 BOM.

<块引用>

我的目标是使用 UTF-8 作为编码和 8859-1 作为字符集创建一个文件

遗憾的是,无论您是否编写 UTF-8,这都是不可能的.IE.只要您编写的字符出现在 ISO Latin-1 中,它就会看起来像一个 ISO 8859-1 文件,但是一旦您输出一个 ISO 8859-1 未涵盖的字符(例如 ä,ö, ü) 这些字符将被写入多字节字符.

要编写真正的 ISO-8859-1,请使用:

Encoding isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1");

在 balexandre 发表评论后

我使用以下代码进行测试...

var filePath = @"c:	emp	est.txt";var sb = new StringBuilder();sb.Append("dsfaskd jlsadfj laskjdflasjdf asdkfjalksjdf lkjdsfljas dddd jflasjdflkjasdlfkjasldfl asääääjdflkaslj df");编码 isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1");TextWriter tw = new StreamWriter(filePath, false, isoLatin1Encoding);tw.WriteLine(sb.ToString());tw.Close();

而且文件看起来非常好.显然,读取文件时应该使用相同的编码.

I tried this aproach without any success

the code I'm using:

// File name
String filename = String.Format("{0:ddMMyyHHmm}", dtFileCreated);
String filePath = Path.Combine(Server.MapPath("App_Data"), filename + ".txt");

// Process       
myObject pbs = new myObject();         
pbs.GenerateFile();

// pbs.GeneratedFile is a StringBuilder object

// Save file
Encoding utf8WithoutBom = new UTF8Encoding(true);
TextWriter tw = new StreamWriter(filePath, false, utf8WithoutBom);
foreach (string s in pbs.GeneratedFile.ToArray()) 
    tw.WriteLine(s);
tw.Close();

// Push Generated File into Client
Response.Clear();
Response.ContentType = "application/vnd.text";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".txt");
Response.TransmitFile(filePath);
Response.End();

the result:

It's writing the BOM no matter what, and special chars (like Æ Ø Å) are not correct :-/

I'm stuck!

My objective is create a file using UTF-8 as Encoding and 8859-1 as CharSet

Is this so hard to accomplish or I'm just getting a bad day?

All help is greatly appreciated, thank you!

解决方案

Well it writes the BOM because you are instructing it to, in the line

Encoding utf8WithoutBom = new UTF8Encoding(true);

true means that the BOM should be emitted, using

Encoding utf8WithoutBom = new UTF8Encoding(false);

writes no BOM.

My objective is create a file using UTF-8 as Encoding and 8859-1 as CharSet

Sadly, this is not possible, either you write UTF-8 or not. I.e. as long as the characters you are writing are present in ISO Latin-1 it will look like a ISO 8859-1 file, however as soon as you output a character that is not covered by ISO 8859-1 (e.g. ä,ö, ü) these characters will be written as a multibyte character.

To write true ISO-8859-1 use:

Encoding isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1");

Edit: After balexandre's comment

I used the following code for testing ...

var filePath = @"c:	emp	est.txt";
var sb = new StringBuilder();
sb.Append("dsfaskd jlsadfj laskjdflasjdf asdkfjalksjdf lkjdsfljas dddd jflasjdflkjasdlfkjasldfl asääääjdflkaslj d f");

Encoding isoLatin1Encoding = Encoding.GetEncoding("ISO-8859-1");

TextWriter tw = new StreamWriter(filePath, false, isoLatin1Encoding);
tw.WriteLine(sb.ToString());
tw.Close();

And the file looks perfectly well. Obviously, you should use the same encoding when reading the file.

这篇关于创建没有 BOM 的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆