Filestream只读取文件的前4个字符 [英] Filestream only reading the first 4 characters of the file
问题描述
| zl
而不是整个流。请注意,它中有一些特殊字符。 有人知道问题是什么吗?这是我的代码:
使用(FileStream fs = File.OpenRead(path))
{
byte [] buffer = new byte [fs.Length];
fs.Read(buffer,0,buffer.Length);
extract = Encoding.Default.GetString(buffer);
}
编辑:
我试图读取所有文字但是它仍然返回相同的四个字符。它可以在任何其他文件上正常工作,除了这几个。当我使用read all行时,它只获得第一行。
fs.Read()
不会同时读取整个字节的碎片,它读取一些字节数并返回读取的字节数。 MSDN有一个很好的例子,说明如何使用它来获取整个文件:
http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx
对于它的价值,将整个150MB的数据读入内存实际上会耗尽您客户的系统 - 首选的选择是优化它以便您不需要整个一次归档。
Hey there! I'm trying to read a 150mb file with a file stream but every time I do it all I get is: |zl
instead of the whole stream. Note that it has some special characters in it.
Does anybody know what the problem could be? here is my code:
using (FileStream fs = File.OpenRead(path))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
extract = Encoding.Default.GetString(buffer);
}
Edit:
I tried to read all text but it still returned the same four characters. It works fine on any other file except for these few. When I use read all lines it only gets the first line.
fs.Read()
does not read the whole smash of bytes all at once, it reads some number of bytes and returns the number of bytes read. MSDN has an excellent example of how to use it to get the whole file:
http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx
For what it's worth, reading the entire 150MB of data into memory is really going to put a drain on your client's system -- the preferred option would be to optimize it so that you don't need the whole file all at once.
这篇关于Filestream只读取文件的前4个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!