BinaryWriter and BinaryReader usage

Source: Internet
Author: User

C # 's FileStream class provides the most primitive byte-level file read and write functionality, but we are accustomed to manipulating strings, so the StreamWriter and StreamReader classes enhance the FileStream, which lets us manipulate files at the string level, But sometimes we still need to manipulate the file at the byte level, but not a byte-by-bit operation, usually 2, 4, or 8 bytes, which has the BinaryWriter and BinaryReader classes, which can write a character or number in a specified number of bytes. You can also read a specified number of bytes to a character or number at a time.

Let's look at an example:

The BinaryWriter and BinaryReader classes are used to read and write data, rather than strings. The following code example demonstrates how to write data to and read from a new, empty file stream (test.data). Once the data file has been created in the current directory, the associated BinaryWriter and BinaryReaderare created, andBinaryWriter is used to Test.data writes an integer 0 to ten,test.data The file pointer at the end of the file. After the file pointer is set back to its initial position,BinaryReader reads the specified content.

 using system;using system.io;class mystream {Private Const string file_name = "Test.data";        public static void Main (string[] args) {//Create the new, empty data file.            if (file.exists (file_name)) {Console.WriteLine ("{0} already exists!", file_name);        Return        } FileStream fs = new FileStream (file_name, filemode.createnew);        Create the writer for data.        BinaryWriter w = new BinaryWriter (FS);        Write data to Test.data.        for (int i = 0; i < one; i++) {w.write ((int) i);        } w.close (); Fs.        Close ();        Create the reader for data.        FS = new FileStream (file_name, FileMode.Open, FileAccess.Read);        BinaryReader r = new BinaryReader (FS);        Read data from Test.data.        for (int i = 0; i < one; i++) {Console.WriteLine (R.readint32 ());        } r.close (); Fs.    Close (); }}

For members of this class see

Http://msdn.microsoft.com/zh-cn/library/system.io.binaryreader_members%28v=vs.80%29.aspx

BinaryWriter and BinaryReader usage

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.