C # Stream

Source: Internet
Author: User

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;//Flownamespacestream{classProgram {Static voidMain (string[] args)            {Stream2 ();        Console.readkey (); }        Static voidStream1 () {stringFilePath ="D:\\test.txt"; //create a FileStream entity with File.Open            using(FileStream FileStream =File.Open (FilePath, FileMode.Open)) {                //Defining Strings                stringmsg ="Hello World";//string msg =console.readline (); //Convert a string to byte data                byte[] Msgasbytearray =Encoding.Default.GetBytes (msg); Console.WriteLine ("start writing to file {0}", MSG); //write byte data to the streamFileStream. Write (Msgasbytearray,0, msgasbytearray.length); //resets the position in the streamFileStream. Seek (0, Seekorigin.begin); Console.WriteLine ("the data written in the file is:"); //Defining byte Arrays                byte[] Bytesformfile =New byte[Msgasbytearray.length]; //read from stream to byte dataFileStream. Read (Bytesformfile,0, msgasbytearray.length);            Console.WriteLine (Encoding.Default.GetString (Bytesformfile)); }        }        /// <summary>        ///StreamWriter and StreamReader/// </summary>        Static voidStream2 () {stringFilePath ="D:\\test.txt"; using(FileStream FileStream =File.Open (FilePath, FileMode.Open)) {                //Defining Strings                stringmsg ="Hello World (Hello)";//string msg =console.readline (); //Create StreamWriter Object System.Text.Encoding.Default: Encoding formatStreamWriter StreamWriter =NewStreamWriter (Filestream,system.text.encoding.default); Console.WriteLine ("start writing {0} to file", MSG); StreamWriter.                Write (msg); //Create a StreamReader objectStreamReader StreamReader =NewStreamReader (FileStream, Encoding.default); //The data is not read here because it is not yet written to completeConsole.WriteLine ("The data written to the file is {0}", StreamReader.                ReadToEnd ()); //CloseStreamWriter.                Close (); StreamReader.            Close (); }        }    }}

Stream Inheritance Structure:

C # Stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.