其他分享
首页 > 其他分享> > 【.NET流操作】TextReader、StreamReader、TextWriter、StreamWriter

【.NET流操作】TextReader、StreamReader、TextWriter、StreamWriter

作者:互联网

TextReader

抽象类,它是StreamReader的父类,

TextReader重要成员

案例

 string text = "abc\nabc";


            using (TextReader reader = new StringReader(text))
            {
                while (reader.Peek() != -1)
                {
                    Console.WriteLine("Peek = {0}", (char)reader.Peek());
                    Console.WriteLine("Read = {0}", (char)reader.Read());
                }
                reader.Close();
            }

            using (TextReader reader = new StringReader(text))
            {
                char[] charBuffer = new char[3];
                int data = reader.ReadBlock(charBuffer, 0, 3);
                for (int i = 0; i < charBuffer.Length; i++)
                {
                    Console.WriteLine("通过readBlock读出的数据:{0}", charBuffer[i]);
                }
                reader.Close();
            }

            using (TextReader reader = new StringReader(text))
            {
                string lineData = reader.ReadLine();
                Console.WriteLine("第一行的数据为:{0}", lineData);
                reader.Close();
            }

            using (TextReader reader = new StringReader(text))
            {
                string allData = reader.ReadToEnd();
                Console.WriteLine("全部的数据为:{0}", allData);
                reader.Close();
            }

            Console.ReadLine();

输出结果:

StreamReader

StreamReader 的定义:实现一个 TextReader,使其以一种特定的编码从字节流中读取字符。

构造函数

属性

FileStream fs = new FileStream ( "D:\\TextReader.txt", FileMode.Open , FileAccess.Read ) ; 
StreamReader sr= new StreamReader ( fs ) ; 
//本例中的BaseStream就是FileStream
sr.BaseStream.Seek (0 , SeekOrigin.Begin ) ;

案例

static void Main(string[] args)
        {
           
            //文件地址
            string txtFilePath="D:\\TextReader.txt";
            //定义char数组
            char[] charBuffer2 = new char[3];

            //利用FileStream类将文件文本数据变成流然后放入StreamReader构造函数中
            using(FileStream stream = File.OpenRead(txtFilePath))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    //StreamReader.Read()方法
                    DisplayResultStringByUsingRead(reader);
                }
            }

            using (FileStream stream = File.OpenRead(txtFilePath))
            {
                //使用Encoding.ASCII来尝试下
                using (StreamReader reader = new StreamReader(stream,Encoding.ASCII,false))
                {
                    //StreamReader.ReadBlock()方法
                    DisplayResultStringByUsingReadBlock(reader);
                }
            }

            //尝试用文件定位直接得到StreamReader,顺便使用 Encoding.Default
            using(StreamReader reader = new StreamReader(txtFilePath, Encoding.Default,false,123))
            {
               //StreamReader.ReadLine()方法
              DisplayResultStringByUsingReadLine(reader);
            }

            //也可以通过File.OpenText方法直接获取到StreamReader对象
            using (StreamReader reader = File.OpenText(txtFilePath)) 
            {
                //StreamReader.ReadLine()方法
                DisplayResultStringByUsingReadLine(reader);
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 使用StreamReader.Read()方法
        /// </summary>
        /// <param name="reader"></param>
        public static  void DisplayResultStringByUsingRead(StreamReader reader) 
        {
            int readChar = 0;
            string result = string.Empty;
            while ((readChar=reader.Read()) != -1) 
            {
                result += (char)readChar;
            }
            Console.WriteLine("使用StreamReader.Read()方法得到Text文件中的数据为 : {0}", result);
        }

        /// <summary>
        /// 使用StreamReader.ReadBlock()方法
        /// </summary>
        /// <param name="reader"></param>
        public static void DisplayResultStringByUsingReadBlock(StreamReader reader)
        {
            char[] charBuffer = new char[10];
            string result = string.Empty;
            reader.ReadBlock(charBuffer,0,10);
            for (int i = 0; i < charBuffer.Length; i++)
            {
                result += charBuffer[i];
            }
            Console.WriteLine("使用StreamReader.ReadBlock()方法得到Text文件中前10个数据为 : {0}", result);
        }


        /// <summary>
        /// 使用StreamReader.ReadLine()方法
        /// </summary>
        /// <param name="reader"></param>
        public static void DisplayResultStringByUsingReadLine(StreamReader reader)
        {
            int i=1;
            string resultString = string.Empty;
            while ((resultString=reader.ReadLine() )!= null)
            {
                Console.WriteLine("使用StreamReader.Read()方法得到Text文件中第{1}行的数据为 : {0}", resultString, i);
                i++;
            }
            
        }

TextWriter

StreamWriter

标签:StreamWriter,TextReader,string,int,char,TextWriter,reader,StreamReader
来源: https://www.cnblogs.com/fanfan-90/p/16343420.html