【发布时间】:2010-11-13 10:09:40
【问题描述】:
我正在使用 filestream 类读取一个包含单行的简单文本文件。但似乎 filestream.read 在开头添加了一些垃圾字符。
代码下方。
using (var _fs = File.Open(_idFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
byte[] b = new byte[_fs.Length];
UTF8Encoding temp = new UTF8Encoding(true);
while (_fs.Read(b, 0, b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
Console.WriteLine(ASCIIEncoding.ASCII.GetString(b));
}
}
例如:我在文本文件中的数据只是“样本”。但是上面的代码返回
"?sample" and
"???sample"
这是什么原因??它是文件指示器的开始吗?有没有办法只阅读我的实际内容??
【问题讨论】:
-
我的文本文件中的数据只是“样本”
-
@CodeInChaos,感谢您的提示,在十六进制编辑中,它看起来是“ef bb bf 73 61 6d 70 6c 65 00 and ...sample”。这是为什么.....
-
如果您需要剥离 BOM,那么您已经做错了。见stackoverflow.com/questions/1317700/…。
标签: c# filestream