在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ C#/ C# StreamReader類
C#屬性(Properties)
C#與Java比較
C#方法
C#枚舉
C#關鍵字
C# StreamReader類
C#不安全代碼
C#文件(I/O)
C#匿名方法
C#線程同步
C# Thread類
C#主線程
C#數(shù)據(jù)類型
C# FileStream類
C#預處理指令
C#繼承
C#循環(huán)
C#決策結構
C#集合
C#反射
C#類型轉(zhuǎn)換
C#泛型
C# StringReader類
C#歷史
C#運算符重載
C#屬性
C#線程實例:Sleep()方法
C#線程示例:優(yōu)先級
C#線程實例:Join()方法
C# BinaryReader類
C#類
C#索引器
C# BinaryWriter類
C#序列化
C#常量和文字
C#程序結構
C#封裝
C#事件
C#可空類型(nullable)
C#基本語法
C#異常處理
C#教程
C#接口
C# System.IO命名空間
C#線程命名實例
C# StringWriter類
C#線程實例
C#數(shù)組
C#正則表達式
C#命名空間
C#反序列化
C#與C++比較
C# TextWriter類
C#多線程
C#字符串
C#是什么?
C#變量
C# FileInfo類
C#線程實例:Abort()方法
C#結構體
C#運算符
C#入門程序
C#多線程生命周期
C# TextReader類
C# DirectoryInfo類
C#委托

C# StreamReader類

C# StreamReader類用于從流中讀取字符串。它繼承自TextReader類,它提供Read()ReadLine()方法從流中讀取數(shù)據(jù)。

C# StreamReader示例:讀取一行

下面來看看一個StreamReader類的簡單例子,它從文件中讀取一行數(shù)據(jù)。

using System;
using System.IO;
public class StreamReaderExample
{
    public static void Main(string[] args)
    {
        FileStream f = new FileStream("e:\\myoutput.txt", FileMode.OpenOrCreate);
        StreamReader s = new StreamReader(f);

        string line = s.ReadLine();
        Console.WriteLine(line);
        string line2 = s.ReadLine();
        Console.WriteLine(line2);
        s.Close();
        f.Close();
    }
}

假設e:\myoutput.txt文件的內(nèi)容如下 -

This is line 1
This is line 2
This is line 3
This is line 4

執(zhí)行上面示例代碼,得到以下輸出結果 -

This is line 1
This is line 2

C# StreamReader示例:讀取所有行

下面代碼演示如何使用 StreamReader 來讀取文件:myoutput.txt中的所有行內(nèi)容 -

using System;
using System.IO;
public class StreamReaderExample
{
    public static void Main(string[] args)
    {
        FileStream f = new FileStream("e:\\myoutput.txt", FileMode.OpenOrCreate);
        StreamReader s = new StreamReader(f);

        string line = "";
        while ((line = s.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
        s.Close();
        f.Close();
    }
}

執(zhí)行上面示例代碼,得到以下輸出結果 -

This is line 1
This is line 2
This is line 3
This is line 4