C#:study(12)--IO

  1. 字节流(抽象类System.IO.Stream的子类)
    BufferedStream 包装字节流并加入缓冲,可以提高性能
    FileStream   文件IO字节流
    MemoryStream 使用内存存储的字节流
  2. 字符流(抽象类TextReader和TextWriter的子类)
    StreamReader 从一个字节流中读取字符,这个类包装了一个字节输出流。
    StreamWriter 向一个字节流写字符,这个类包装了一个字节输出流。
    StringReader 从一个字符串中读取字符。
    StringWriter 向一个字符串中写字符。
  3. 二进制流
    BinaryReader,BinaryWriter。
  4.  预定义流(字符流的实现)
    Console.In、Console.Out、Console.Error

  • 重定向标准流
      1.  在命令行
        Test.exe > log.txt
      2. 在程序中
        Console.SetIn();Console.SetOut();Console.SetError();
  • 随机访问文件
    long Seek(long newPos,seekOrigin origin);
  • 编码
    System.Text.Encoding
          System.Text.ASCIIEncoding
          System.Text.UnicodeEncoding
          System.Text.UTF7Encoding
          System.Text.UTF8Encoding
     1 using System;
     2 using System.Text;
     3 
     4 namespace ConvertExample
     5 {
     6    class ConvertExampleClass
     7    {
     8       static void Main()
     9       {
    10          string unicodeString = "This string contains the unicode character Pi(\u03a0)";
    11 
    12          // Create two different encodings.
    13          Encoding ascii = Encoding.ASCII;
    14          Encoding unicode = Encoding.Unicode;
    15 
    16          // Convert the string into a byte[].
    17          byte[] unicodeBytes = unicode.GetBytes(unicodeString);
    18 
    19          // Perform the conversion from one encoding to the other.
    20          byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
    21             
    22          // Convert the new byte[] into a char[] and then into a string.
    23          // This is a slightly different approach to converting to illustrate
    24          // the use of GetCharCount/GetChars.
    25          char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
    26          ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
    27          string asciiString = new string(asciiChars);
    28 
    29          // Display the strings created before and after the conversion.
    30          Console.WriteLine("Original string: {0}", unicodeString);
    31          Console.WriteLine("Ascii converted string: {0}", asciiString);
    32       }
    33    }
    34 }
    35 



posted @ 2005-08-22 09:23  zhh007's Bolg  阅读(249)  评论(0编辑  收藏  举报