Csharp/C Sharp by API/System.IO/StreamReader

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

new StreamReader(FileStream fs)

 

using System;
using System.IO;
class MainClass {
    public static void Main(string[] args) {
        try {
            FileStream fs = new FileStream("c:\\a.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs);
            string line = "";
            int lineNo = 0;
            do {
                line = sr.ReadLine();
                if (line != null) {
                    Console.WriteLine("{0}: {1}", lineNo, line);
                    lineNo++;
                }
            } while (line != null);
        } catch (Exception e) {
            Console.WriteLine("Exception in ShowFile: {0}", e);
        }
    }
}


new StreamWriter(fs, Encoding.UTF8)

 
using System;
using System.IO;
using System.Text;
class MainClass {
    static void Main() {
        using (FileStream fs = new FileStream("test.txt", FileMode.Create)) {
            using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) {
                w.WriteLine(124.23M);
                w.WriteLine("Test string");
                w.WriteLine("!");
            }
        }
        using (FileStream fs = new FileStream("test.txt", FileMode.Open)) {
            using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) {
                Console.WriteLine(Decimal.Parse(r.ReadLine()));
                Console.WriteLine(r.ReadLine());
                Console.WriteLine(Char.Parse(r.ReadLine()));
            }
        }
    }
}


new StreamWriter(String filePath)

 
using System;
using System.IO;

public class MyStreamWriterReader {
    static void Main(string[] args) {
        StreamWriter writer = new StreamWriter("reminders.txt");
        writer.WriteLine("...");
        writer.WriteLine("Don"t forget these numbers:");
        for (int i = 0; i < 10; i++)
            writer.Write(i + " ");
        writer.Write(writer.NewLine);
        writer.Close();
        StreamReader sr = new StreamReader("reminders.txt");
        string input = null;
        while ((input = sr.ReadLine()) != null) {
            Console.WriteLine(input);
        }
    }
}


StreamReader.Peek()

 
using System;
using System.IO;
    public class StrmRdr
    {
        static public void Main (string [] args)
        {
            FileStream strm;
            StreamReader reader;
            strm = new FileStream ("test.txt", FileMode.Open, FileAccess.Read);
            reader = new StreamReader (strm);
            while (reader.Peek() >= 0)
            {
                string text = reader.ReadLine ();
                Console.WriteLine (text);
            }
            reader.Close ();
            strm.Close ();
        }
    }


StreamReader.ReadLine()

 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
class Program {
    static void Main(string[] args) {
        string strLine;
        try {
            FileStream aFile = new FileStream("Log.txt", FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            strLine = sr.ReadLine();
            while (strLine != null) {
                Console.WriteLine(strLine);
                strLine = sr.ReadLine();
            }
            sr.Close();
        } catch (IOException e) {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(e.ToString());
            return;
        }
    }
}


StreamReader.ReadToEnd()

 
using System;
using System.IO;
  public class StreamReaderAndWriter {
    static void Main(string[] args)
    {
      StreamReader MyStreamReader = new StreamReader(@"c:\Testing.txt");
      FileStream MyFileStream = new FileStream(@"c:\Testing.txt", FileMode.Open, FileAccess.Read, FileShare.None);
      StreamReader MyStreamReader2 = new StreamReader(MyFileStream);
      MyFileStream.Close();
      MyStreamReader2.Close();
      string MyStringReader = MyStreamReader.ReadLine();
      string MyStringReadToEOF = MyStreamReader.ReadToEnd();
      int[] MyArrayOfCharacters = new int[100];
      for (int i = 0; i < 99; i++)
      {
        MyArrayOfCharacters[i] = MyStreamReader.Read();
      }
      MyStreamReader.Close();
    }
  }