System.IO.StreamReader.Read 方法

方法描述

读取输入流中的下一个字符并使该字符的位置提升一个字符。

语法定义(C# System.IO.StreamReader.Read 方法 的用法)

public override int Read()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.Int32 输入流中表示为 Int32 对象的下一个字符。如果不再有可用的字符,则为 -1。

提示和注释

此方法重写 Read。

此方法返回一个整数,以便在到达流的末尾时可以返回 -1。 如果在将数据读取到缓冲区后操作基础流的位置,则基础刘的位置可能不匹配内部缓冲区的位置。 要重置内部缓冲区,请调用 DiscardBufferedData方法;但是,此方法会降低性能,并且只应在绝对必要时调用。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

System.IO.StreamReader.Read 方法例子

下面的代码示例演示如何使用 Read() 方法重载读取单个字符,并将 ASCII 整数的输出格式设置为十进制和十六进制数。

using System;
using System.IO;

class StrmRdrRead
{
public static void Main()
    {
    //Create a FileInfo instance representing an existing text file.
    FileInfo MyFile=new FileInfo(@"c:\csc.txt");
    //Instantiate a StreamReader to read from the text file.
    StreamReader sr=MyFile.OpenText();
    //Read a single character.
    int FirstChar=sr.Read();
    //Display the ASCII number of the character read in both decimal and hexadecimal format.
    Console.WriteLine("The ASCII number of the first character read is {0:D} in decimal and {1:X} in hexadecimal.",
        FirstChar, FirstChar);
    //
    sr.Close();
    }
}

异常

异常 异常描述
IOException 发生 I/O 错误。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。