有什么区别:文件结束(EOF),流结束和字符串!= C#中的NULL

时间:2013-04-02 06:54:42

标签: c# file-io

使用之间有什么区别 文件处理中的StreamReader.EndOfStreamstring != null

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}






 while (!reader.EndOfStream)
            {
                data = BreakLine(line);
                DataRow dr = _DataTable.NewRow();
                // protect against overflow
                int maxColumns = Math.Min(_DataTable.Columns.Count, data.Length);
                for (int i = 0; i < maxColumns; i++)
                {
                    dr[i] = data[i];
                }
                _DataTable.Rows.Add(dr);
                line = reader.ReadLine();
            }

1 个答案:

答案 0 :(得分:1)

这两个处理不同的情况。如果您对文件进行操作,EOF将指示您的文件指针到达文件的末尾,而Stream是连续的数据流(通常是单向的,而不是文件你通常可以随机访问)。

来自here它说文件

"The easiest way to think about a file is as a linear sequence of characters."

和它说的流

"We can think of a stream as a channel or conduit on which data is passed from senders to receivers."

阅读文章以获得印象。虽然它大约是C++,但它很好地反映了基础知识。