读取文件的最快方法 [英] Fastest way to read a file

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

问题描述

有人知道什么是(事实上请 - 不要猜测)从文件中读取数据的最快

方法?我对

数据(即块,字节,字符串等)的格式不感兴趣,只是因为读取数据的IO很快就是
。我目前正在使用Streamreader并且已经发现readline

方法的性能略好于read方法(尽管很好

来读取'的'粒度'一个字节)。有没有更快的阅读器

我可以使用? (只对读取感兴趣 - 不写)。


谢谢。


马克。

解决方案

Mark Broadbent写道:

有人知道什么是(事实请 - 不要猜测)从文件中读取数据的最快方法吗?我对
数据的格式(即块,字节,字符串等)不感兴趣,因为读取数据的IO非常快。我目前正在使用Streamreader并且发现readline
方法的性能略好于read方法(尽管读取'的一个字节的粒度是很好的)。是否有更快的读者可以使用? (只对读取感兴趣 - 不写)。

谢谢。

Mark。




不确定这是否是最快但你可以尝试一下。

公共静态字符串FileToStr(字符串cFileName)

{

//创建一个StreamReader并打开文件

StreamReader oReader = System.IO.File.OpenText(cFileName);


//读取文件的所有内容在一个字符串中

string lcString = oReader.ReadToEnd();


//关闭StreamReader并返回字符串

oReader .Close();

返回lcString;

}


嗨Mark,

您没有告诉我们您的数据格式是什么。如果它是原始数据,那么StreamReader会将其视为文本,并且可以更快地从FileStream中读取。我尝试使用~30mb二进制文件,StreamReader花了大约三倍的时间来读取所有字节。


但是,由于你使用StreamReader开始,你可能有一个文本文件。尽管如此,尝试使用FileStream.Read将其作为字节数组读取并使用Encoding类将字节转换为字符串可能是值得的。

On Thu,2005年7月28日11:59:35 +0200 ,Mark Broadbent< no **** @ nospam.com>写道:

有人知道什么是(事实上请 - 不要只是猜测)从文件中读取数据的最快方法吗?我对
数据的格式(即块,字节,字符串等)不感兴趣,因为读取数据的IO非常快。我目前正在使用Streamreader并且发现readline
方法的性能略好于read方法(尽管读取'的一个字节的粒度是很好的)。是否有更快的读者可以使用? (只对读取感兴趣 - 不写)。

谢谢。

Mark。




-

快乐的编码!

Morten Wennevik [C#MVP]


只需2美分。我会使用使用声明包装StreamReader

对象。这样它即使打开文件中的异常或

读取也将被关闭。

类似(来自内存):


public static string ReadFile(string path)

{

using(StreamReader sr = new StreamReader(path))

{

返回sr.ReadToEnd();

}

}


-

William Stacey [MVP]


" Eugene Vtial" <是ne ** @ microsoft.com>在消息中写道

news:eH ************** @ TK2MSFTNGP09.phx.gbl ...

Mark Broadbent写道:< blockquote class =post_quotes>有人知道从文件中读取数据的最快的方法是什么(事实请 - 不要猜测)?我对
数据的格式(即块,字节,字符串等)不感兴趣,因为读取数据的IO非常快。我目前正在使用Streamreader,并且发现
readline
方法的性能略好于read方法(尽管它很好
让读取的粒度为一个字节)。是否有更快的读者
我可以使用? (只对读取感兴趣 - 不写)。

谢谢。

马克。



不确定是否这样是最快但你可以尝试一下。

公共静态字符串FileToStr(字符串cFileName)
//
//创建StreamReader并打开文件
StreamReader oReader = System.IO.File.OpenText(cFileName);

//用字符串读取文件的所有内容
string lcString = oReader.ReadToEnd();

//关闭StreamReader并返回字符串
oReader.Close();
返回lcString;
}



Does anybody know what is (factual please -not just guess) the quickest
method to read data from a file? I am not interested in the format of the
data (i.e. blocks, bytes, string etc) just that the IO to read the data is
very quick. I am currently using a Streamreader and have found the readline
method to perform slightly better than the read method (although it is nice
to have the read''s granuality of one byte). Is there any faster reader that
I can use? (only interested in reads -not writes).

Thanks.

Mark.

解决方案

Mark Broadbent wrote:

Does anybody know what is (factual please -not just guess) the quickest
method to read data from a file? I am not interested in the format of the
data (i.e. blocks, bytes, string etc) just that the IO to read the data is
very quick. I am currently using a Streamreader and have found the readline
method to perform slightly better than the read method (although it is nice
to have the read''s granuality of one byte). Is there any faster reader that
I can use? (only interested in reads -not writes).

Thanks.

Mark.



Not sure if this is the fastest but you could give it a try.
public static string FileToStr(string cFileName)
{
//Create a StreamReader and open the file
StreamReader oReader = System.IO.File.OpenText(cFileName);

//Read all the contents of the file in a string
string lcString = oReader.ReadToEnd();

//Close the StreamReader and return the string
oReader.Close();
return lcString;
}


Hi Mark,

You''re not telling us what format your data is in. If it is raw data, then StreamReader will treat it as text and it will be faster reading directly from the FileStream. I tried with ~30mb binary file and StreamReader took roughly three times longer to read all the bytes.

However, since you are using StreamReader to begin with you probably have a text file. Still, it might be worth trying to read it as a byte array with FileStream.Read and convert the bytes to string using the Encoding class.
On Thu, 28 Jul 2005 11:59:35 +0200, Mark Broadbent <no****@nospam.com> wrote:

Does anybody know what is (factual please -not just guess) the quickest
method to read data from a file? I am not interested in the format of the
data (i.e. blocks, bytes, string etc) just that the IO to read the data is
very quick. I am currently using a Streamreader and have found the readline
method to perform slightly better than the read method (although it is nice
to have the read''s granuality of one byte). Is there any faster reader that
I can use? (only interested in reads -not writes).

Thanks.

Mark.



--
Happy coding!
Morten Wennevik [C# MVP]


Just 2 cents. I would use a "using" statement to wrap the StreamReader
object. That way it will be closed even if exception in opening file or in
the read.
Something like (from memory):

public static string ReadFile(string path)
{
using(StreamReader sr = new StreamReader(path))
{
return sr.ReadToEnd();
}
}

--
William Stacey [MVP]

"Eugene Vtial" <ne**@microsoft.com> wrote in message
news:eH**************@TK2MSFTNGP09.phx.gbl...

Mark Broadbent wrote:

Does anybody know what is (factual please -not just guess) the quickest
method to read data from a file? I am not interested in the format of the
data (i.e. blocks, bytes, string etc) just that the IO to read the data
is
very quick. I am currently using a Streamreader and have found the
readline
method to perform slightly better than the read method (although it is
nice
to have the read''s granuality of one byte). Is there any faster reader
that
I can use? (only interested in reads -not writes).

Thanks.

Mark.



Not sure if this is the fastest but you could give it a try.
public static string FileToStr(string cFileName)
{
//Create a StreamReader and open the file
StreamReader oReader = System.IO.File.OpenText(cFileName);

//Read all the contents of the file in a string
string lcString = oReader.ReadToEnd();

//Close the StreamReader and return the string
oReader.Close();
return lcString;
}



这篇关于读取文件的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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