Csharp/CSharp Tutorial/File Directory Stream/TextReader TextWriter

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

TextWriter defines Write() and WriteLine() that output all of the built-in types.

Method Description void Write(int val) Write an int. void Write(double val) Write a double. void Write(bool val) Write a bool. void WriteLine(string val) Write a string followed by a newline. void WriteLine(uint val) Write a uint followed by a newline. void WriteLine(char val) Write a character followed by a newline. void Close() Close the stream. void Flush() Write any data remaining in the output buffer to the physical medium.

The Input Methods Defined by TextReader

Method Description void Close() Closes the input source. int Peek() Gets the next character from the input stream without bypassing that character. Returns -1 if no character is available. int Read() Returns an integer representing the next character. Returns -1 when the end of the stream is encountered. int Read(char[ ] buf,int offset, int numChars) Attempts to read up to numChars characters into buf starting at buf[offset], returning the number of characters successfully read. int ReadBlock(char[ ] buf,int offset, int numChars) Attempts to read up to numChars characters into buf starting at buf[offset], returning the number of characters successfully read. string ReadLine() Reads the next line of text. Returns Null if it is at end-of-file. string ReadToEnd() Reads all of the remaining characters in a stream.

The TextReader and TextWriter classes are implemented by several character-based stream classes.

Stream Class Description StreamReader Read characters from a byte stream. This class wraps a byte input stream. StreamWriter Write characters to a byte stream. This class wraps a byte output stream. StringReader Read characters from a string. StringWriter Write characters to a string.

Writing to a writer

<source lang="csharp">using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.IO.rupression; using System.Net; using System.Net.Mail; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; public class MainClass {

   public static void Main()
   {
       using (TextWriter tw = Console.Out)
       {
           tw.Write(302.30m); 
           tw.Write("str"); 
           tw.Write(true); 
           tw.WriteLine(".");
       }
   }

}</source>

302.30strTrue.