From MSDN 既然用的是 ms 的东西,msdn这么好的资料库都不用?.NET Framework 类库 StreamReader.Read 方法 [C#]请参见 StreamReader 类 | StreamReader 成员 | System.IO 命名空间 | C++ 托管扩展编程 语言 C#C++JScriptVisual Basic全部显示 读取输入流中的下一个字符或下一组字符。重载列表 读取输入流中的下一个字符并使该字符的位置提升一个字符。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Overrides Public Function Read() As Integer [C#] public override int Read(); [C++] public: int Read(); [JScript] public override function Read() : int; 从 index 开始,从当前流中将最多的 count 个字符读入 buffer。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Overrides Public Function Read(Char(), Integer, Integer) As Integer [C#] public override int Read(char[], int, int); [C++] public: int Read(__wchar_t __gc[], int, int); [JScript] public override function Read(Char[], int, int) : int; 示例 [Visual Basic, C#, C++] 下面的示例一次读取五个字符直至达到文件的末尾。[Visual Basic, C#, C++] 注意 此示例显示如何使用 Read 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。 [Visual Basic] Imports System Imports System.IO Imports System.TextPublic Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" Try If File.Exists(path) Then File.Delete(path) End If Dim sw As StreamWriter = New StreamWriter(path) sw.WriteLine("This") sw.WriteLine("is some text") sw.WriteLine("to test") sw.WriteLine("Reading") sw.Close() Dim sr As StreamReader = New StreamReader(path) Do While sr.Peek() >= 0 'This is an arbitrary size for this example. Dim c(5) As Char sr.Read(c, 0, c.Length) 'The output will look odd, because 'only five characters are read at a time. Console.WriteLine(c) Loop sr.Close() Catch e As Exception Console.WriteLine("The process failed: {0}", e.ToString()) End Try End Sub End Class[C#] using System; using System.IO;class Test {
public static void Main() { string path = @"c:\temp\MyTest.txt"; try { if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine("This"); sw.WriteLine("is some text"); sw.WriteLine("to test"); sw.WriteLine("Reading"); } using (StreamReader sr = new StreamReader(path)) { //This is an arbitrary size for this example. char[] c = null; while (sr.Peek() >= 0) { c = new char[5]; sr.Read(c, 0, c.Length); //The output will look odd, because //only five characters are read at a time. Console.WriteLine(c); } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }[C++] #using <mscorlib.dll>using namespace System; using namespace System::IO;void main() { String* path = S"c:\\temp\\MyTest.txt"; try { if (File::Exists(path)) { File::Delete(path); } StreamWriter* sw = new StreamWriter(path); try { sw->WriteLine(S"This"); sw->WriteLine(S"is some text"); sw->WriteLine(S"to test"); sw->WriteLine(S"Reading"); } __finally { if (sw) __try_cast<IDisposable*>(sw)->Dispose(); } StreamReader* sr = new StreamReader(path); try { //This is an arbitrary size for this example. Char c[] = 0; while (sr->Peek() >= 0) { c = new Char[5]; sr->Read(c, 0, c->Length); //The output will look odd, because //only five characters are read at a time. Console::WriteLine(c); } } __finally { if (sr) __try_cast<IDisposable*>(sr)->Dispose(); } } catch (Exception* e) { Console::WriteLine(S"The process failed: {0}", e); } }
既然用的是 ms 的东西,msdn这么好的资料库都不用?.NET Framework 类库 StreamReader.Read 方法 [C#]请参见
StreamReader 类 | StreamReader 成员 | System.IO 命名空间 | C++ 托管扩展编程
语言
C#C++JScriptVisual Basic全部显示
读取输入流中的下一个字符或下一组字符。重载列表
读取输入流中的下一个字符并使该字符的位置提升一个字符。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Overrides Public Function Read() As Integer
[C#] public override int Read();
[C++] public: int Read();
[JScript] public override function Read() : int;
从 index 开始,从当前流中将最多的 count 个字符读入 buffer。受 .NET Framework 精简版的支持。[Visual Basic] Overloads Overrides Public Function Read(Char(), Integer, Integer) As Integer
[C#] public override int Read(char[], int, int);
[C++] public: int Read(__wchar_t __gc[], int, int);
[JScript] public override function Read(Char[], int, int) : int;
示例
[Visual Basic, C#, C++] 下面的示例一次读取五个字符直至达到文件的末尾。[Visual Basic, C#, C++] 注意 此示例显示如何使用 Read 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Imports System
Imports System.IO
Imports System.TextPublic Class Test Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt" Try
If File.Exists(path) Then
File.Delete(path)
End If Dim sw As StreamWriter = New StreamWriter(path)
sw.WriteLine("This")
sw.WriteLine("is some text")
sw.WriteLine("to test")
sw.WriteLine("Reading")
sw.Close() Dim sr As StreamReader = New StreamReader(path) Do While sr.Peek() >= 0
'This is an arbitrary size for this example.
Dim c(5) As Char
sr.Read(c, 0, c.Length)
'The output will look odd, because
'only five characters are read at a time.
Console.WriteLine(c)
Loop
sr.Close()
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
End Class[C#]
using System;
using System.IO;class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; try
{
if (File.Exists(path))
{
File.Delete(path);
} using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("This");
sw.WriteLine("is some text");
sw.WriteLine("to test");
sw.WriteLine("Reading");
} using (StreamReader sr = new StreamReader(path))
{
//This is an arbitrary size for this example.
char[] c = null; while (sr.Peek() >= 0)
{
c = new char[5];
sr.Read(c, 0, c.Length);
//The output will look odd, because
//only five characters are read at a time.
Console.WriteLine(c);
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}[C++]
#using <mscorlib.dll>using namespace System;
using namespace System::IO;void main() {
String* path = S"c:\\temp\\MyTest.txt"; try {
if (File::Exists(path)) {
File::Delete(path);
} StreamWriter* sw = new StreamWriter(path);
try {
sw->WriteLine(S"This");
sw->WriteLine(S"is some text");
sw->WriteLine(S"to test");
sw->WriteLine(S"Reading");
} __finally {
if (sw) __try_cast<IDisposable*>(sw)->Dispose();
} StreamReader* sr = new StreamReader(path);
try {
//This is an arbitrary size for this example.
Char c[] = 0; while (sr->Peek() >= 0) {
c = new Char[5];
sr->Read(c, 0, c->Length);
//The output will look odd, because
//only five characters are read at a time.
Console::WriteLine(c);
}
} __finally {
if (sr) __try_cast<IDisposable*>(sr)->Dispose();
}
} catch (Exception* e) {
Console::WriteLine(S"The process failed: {0}", e);
}
}