Csharp/CSharp Tutorial/File Directory Stream/StreamReader

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

A FileReader

To perform character-based file operations, you wrap a FileStream inside either a StreamReader or a StreamWriter.


using System; 
using System.IO;  
 
class MainClass { 
  public static void Main() { 
    FileStream fin; 
    string s; 
 
    try { 
      fin = new FileStream("test.txt", FileMode.Open); 
    } 
    catch(FileNotFoundException exc) { 
      Console.WriteLine(exc.Message + "Cannot open file."); 
      return ; 
    }  
 
    StreamReader fstr_in = new StreamReader(fin); 
 
    // Read the file line-by-line. 
    while((s = fstr_in.ReadLine()) != null) { 
      Console.WriteLine(s); 
    } 
 
    fstr_in.Close(); 
  } 
}
asdfasdf

Create StreamReader from a URL

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
class MainClass
{
    static void Main(string[] args)
    {
        StreamReader reader = new StreamReader(WebRequest.Create("http://www.nfex.ru").GetResponse().GetResponseStream());
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
}
<HTML>
<HEAD>
     Java examples (example source code) Organized by topic </title>
...

Create StreamReader through FileStream

using System;
using System.IO;
public class MainClass
{
  static void Main(string[] args)
  {
    FileStream MyFileStream = new FileStream(@"c:\Testing.txt", FileMode.Open, FileAccess.Read, FileShare.None);
    StreamReader MyStreamReader = new StreamReader(MyFileStream);

    for (int i = 0; i < 99; i++)
    {
      int ch = MyStreamReader.Read();
      Console.WriteLine(ch);
      
    }
    MyStreamReader.Close();
  }
}

Create StringReader from String

using System;
using System.IO;
using System.Text;
class MainClass
{
  public static void Main() 
  {
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    sw.Write("This is a test of the StringWriter class");
    sw.Close();
    StringReader sr = new StringReader(sb.ToString());
    string EntireString;
  
    EntireString = sr.ReadToEnd();
    Console.WriteLine(EntireString);
    sr.Close();
  }
}
This is a test of the StringWriter class

Demonstrate StringReader and StringWriter

using System; 
using System.IO; 
   
class MainClass {   
  public static void Main() {   
    StringWriter strwtr = new StringWriter(); 
 
    for(int i=0; i < 10; i++) 
       strwtr.WriteLine("This is i: " + i); 
 
    StringReader strrdr = new StringReader(strwtr.ToString()); 
 
    string str = strrdr.ReadLine(); 
    while(str != null) { 
      str = strrdr.ReadLine(); 
      Console.WriteLine(str); 
    }  
  
  }  
}
This is i: 1
This is i: 2
This is i: 3
This is i: 4
This is i: 5
This is i: 6
This is i: 7
This is i: 8
This is i: 9

Read decimal, string and char from a file using FileStream and StreamReader

using System;
using System.IO;
using System.Text;
static class MainClass
{
    static void Main()
    {
        // Create a new file.
        using (FileStream fs = new FileStream("test.txt", FileMode.Create))
        {
            using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
            {
                // Write a decimal, string, and char.
                w.WriteLine(124.23M);
                w.WriteLine("Test string");
                w.WriteLine("A");
            }
        }

        // Open the file in read-only mode.
        using (FileStream fs = new FileStream("test.txt", FileMode.Open))
        {
            using (StreamReader r = new StreamReader(fs, Encoding.UTF8))
            {
                // Read the data and convert it to the appropriate data type.
                Console.WriteLine(Decimal.Parse(r.ReadLine()));
                Console.WriteLine(r.ReadLine());
                Console.WriteLine(Char.Parse(r.ReadLine()));
            }
        }
    }
}
124.23
Test string
A

Read one character at a time

using System;
using System.IO;
public class MainClass
{
  static void Main(string[] args)
  {
    StreamReader MyStreamReader = new StreamReader(@"c:\Projects\Testing.txt");
    for (int i = 0; i < 99; i++)
    {
      int ch = MyStreamReader.Read();
      Console.WriteLine(ch);
    }
    MyStreamReader.Close();
  }
}

StreamReader: ReadLine

using System;
using System.IO;
public class MainClass
{
  static void Main(string[] args)
  {
    StreamReader MyStreamReader = new StreamReader(@"c:\Test.txt");
    string str = MyStreamReader.ReadLine();
    Console.WriteLine(str);
    MyStreamReader.Close();
  }
}

StreamReader: ReadToEnd()

using System;
using System.IO;
public class MainClass
{
  static void Main(string[] args)
  {
    StreamReader MyStreamReader = new StreamReader(@"c:\Projects\Testing.txt");
    string str = MyStreamReader.ReadToEnd();
    Console.WriteLine(str);
    MyStreamReader.Close();
  }
}

Stream Read with StreamReader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

    class Program
    {
        static void Main(string[] args)
        {
            FileStream aFile = new FileStream("Log.txt", FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            string strLine = sr.ReadLine();
            
            while (strLine != null)
            {
                Console.WriteLine(strLine);
                strLine = sr.ReadLine();
            }
            sr.Close();
        }
    }

Use StreamReader to read a line from a text file

using System;
using System.IO;
class MainClass
{
    public static void Main(string[] args)
    {
            
            string filename = "c:\\file.txt";
            
            FileStream f = new FileStream(filename, FileMode.Open);
            StreamReader stream = new StreamReader(f);
            
            string line;
            line = stream.ReadLine();    // header line
            Console.WriteLine(line);
            
    }
}