Latest web development tutorials

Binary file reading and writing C #

Input and Output C # file Input and Output C # file

BinaryReader and BinaryWriterclasses for reading and writing binary files.

BinaryReader class

BinaryReader class for reading binary data from a file.ABinaryReader objects passed to its constructor FileStreamobject is created.

The following table lists some commonBinaryReader class methods:

序号方法 & 描述
1public override void Close()
关闭 BinaryReader 对象和基础流。
2public virtual int Read()
从基础流中读取字符,并把流的当前位置往前移。
3public virtual bool ReadBoolean()
从当前流中读取一个布尔值,并把流的当前位置往前移一个字节。
4public virtual byte ReadByte()
从当前流中读取下一个字节,并把流的当前位置往前移一个字节。
5public virtual byte[] ReadBytes( int count )
从当前流中读取指定数目的字节到一个字节数组中,并把流的当前位置往前移指定数目的字节。
6public virtual char ReadChar()
从当前流中读取下一个字节,并把流的当前位置按照所使用的编码和从流中读取的指定的字符往前移。
7public virtual char[] ReadChars( int count )
从当前流中读取指定数目的字节,在一个字符数组中返回数组,并把流的当前位置按照所使用的编码和从流中读取的指定的字符往前移。
8public virtual double ReadDouble()
从当前流中读取一个 8 字节浮点值,并把流的当前位置往前移八个字节。
9public virtual int ReadInt32()
从当前流中读取一个 4 字节有符号整数,并把流的当前位置往前移四个字节。
10public virtual string ReadString()
从当前流中读取一个字符串。字符串以长度作为前缀,同时编码为一个七位的整数。

For a complete list, please visit the Microsoft C # documents.

BinaryWriter class

BinaryWriter class for writing binary data to a file.ABinaryWriter objects passed to its constructor FileStreamobject is created.

The following table lists some commonBinaryWriter class methods:

序号方法 & 描述
1public override void Close()
关闭 BinaryWriter 对象和基础流。
2public virtual void Flush()
清理当前编写器的所有缓冲区,使得所有缓冲数据写入基础设备。
3public virtual long Seek( int offset, SeekOrigin origin )
设置当前流内的位置。
4public virtual void Write( bool value )
把一个单字节的布尔值写入到当前流中,0 表示 false,1 表示 true。
5public virtual void Write( byte value )
把一个无符号字节写入到当前流中,并把流的位置往前移一个字节。
6public virtual void Write( byte[] buffer )
把一个字节数组写入到基础流中。
7public virtual void Write( char ch )
把一个 Unicode 字符写入到当前流中,并把流的当前位置按照所使用的编码和要写入到流中的指定的字符往前移。
8public virtual void Write( char[] chars )
把一个字符数组写入到当前流中,并把流的当前位置按照所使用的编码和要写入到流中的指定的字符往前移。
9public virtual void Write( double value )
把一个 8 字节浮点值写入到当前流中,并把流位置往前移八个字节。
10public virtual void Write( int value )
把一个 4 字节有符号整数写入到当前流中,并把流位置往前移四个字节。
11public virtual void Write( string value )
把一个以长度为前缀的字符串写入到 BinaryWriter 的当前编码的流中,并把流的当前位置按照所使用的编码和要写入到流中的指定的字符往前移。

For a complete list, please visit the Microsoft C # documents.

Examples

The following example demonstrates reading and writing binary data:

using System;
using System.IO;

namespace BinaryFileApplication
{
    class Program
    {
        static void Main (string [] args)
        {
            BinaryWriter bw;
            BinaryReader br;
            int i = 25;
            double d = 3.14157;
            bool b = true;
            string s = "I am happy";
            // Create a file try
            {
                bw = new BinaryWriter (new FileStream ( "mydata",
				FileMode.Create));
            }
            catch (IOException e)
            {
                Console.WriteLine (e.Message + "\ n Can not create file.");
                return;
            }
            // Try to write files
            {
                bw.Write (i);
                bw.Write (d);
                bw.Write (b);
                bw.Write (s);
            }
            catch (IOException e)
            {
                Console.WriteLine (e.Message + "\ n Can not write to file.");
                return;
            }

            bw.Close ();
            // Try to read the file
            {
                br = new BinaryReader (new FileStream ( "mydata",
				FileMode.Open));
            }
            catch (IOException e)
            {
                Console.WriteLine (e.Message + "\ n Can not open file.");
                return;
            }
            try
            {
                i = br.ReadInt32 ();
                Console.WriteLine ( "Integer data: {0}", i);
                d = br.ReadDouble ();
                Console.WriteLine ( "Double data: {0}", d);
                b = br.ReadBoolean ();
                Console.WriteLine ( "Boolean data: {0}", b);
                s = br.ReadString ();
                Console.WriteLine ( "String data: {0}", s);
            }
            catch (IOException e)
            {
                Console.WriteLine (e.Message + "\ n Can not read from file.");
                return;
            }
            br.Close ();
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Integer data: 25
Double data: 3.14157
Boolean data: True
String data: I am happy

Input and Output C # file Input and Output C # file