[英]Streamwriter adding characters to first line
I am doing something rather simple in c#, writing a list of strings to text file. 我在c#中做的事情很简单,将字符串列表写入文本文件。 My write sub is:
我的写子是:
public static bool TextToFile(string fileName, List<string> inString) {
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
try {
if (File.Exists(fileName))
File.Delete(fileName);
const int BufferSize = 65536; // 64 Kilobytes
using (StreamWriter sw = new StreamWriter(fileName, true, Encoding.UTF8, BufferSize)) {
if (inString.Count > 0) {
foreach (string str in inString) {
sw.WriteLine(str);
}
}
else
sw.WriteLine("");
}
return true;
}
catch {
return false;
}
}
I am getting extra stuff at the beginning of the first line though. 但是,在第一行的开头我得到了更多的东西。 It does not show in a regular text editor, but when I opened in ultraedit, and went to hex mode, I saw this:
它不会在常规文本编辑器中显示,但是当我在ultraedit中打开并进入十六进制模式时,我看到了:
My programs that read the text file do see the characters, and confuse it. 我的读取文本文件的程序确实看到了字符并将其混淆。 My list of strings is super clean.
我的字符串列表非常干净。 I am sometimes writing 100 mb text files, so am setting the buffer to 64k, but I tried leaving it as default with same results.
有时我正在编写100 mb的文本文件,因此将缓冲区设置为64k,但是我尝试将其保留为默认值,结果相同。 I am on win7 64 bit, using VS 2013.
我在使用VS 2013的Win7 64位上。
我遇到了这个确切的问题,并且(由于对此问题的评论)将这些字符标识为字节顺序标记,然后允许我使用以下代码更改编码以排除BOM:
using (StreamWriter sw = new StreamWriter(fileName, true, new UTF8Encoding(false), BufferSize))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.