首页  编辑  

short数组的文件读写方法

Tags: /C#/文件目录操作/   Date Created:

private void button1_Click( object sender , EventArgs e)

{

    short [] buffer = new short [] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 };

    FileStream vFileStream = new FileStream ( @"c:\temp\temp.dat" ,  

        FileMode . Create,   FileAccess . Write);

    byte [] temp = new byte [buffer . Length * sizeof ( short )];

    Buffer . BlockCopy(buffer, 0 , temp, 0 , temp . Length);

   vFileStream . Write(temp, 0 , temp . Length);

   vFileStream . Close();

}

protected void button2_Click( object sender , EventArgs e)

{

    FileStream vFileStream = new FileStream ( @"c:\temp\temp.dat" ,

        FileMode . Open, FileAccess . Read);

    byte [] temp = new byte [vFileStream . Length];

   vFileStream . Read(temp, 0 , temp . Length);

    short [] buffer = new short [temp . Length / sizeof ( short )];

    Buffer . BlockCopy(temp, 0 , buffer, 0 , buffer . Length * sizeof ( short ));

   vFileStream . Close();

   Text = string . Format( "{0},{1},{2}" , buffer[ 0 ], buffer[ 1 ], buffer[ 2 ]);

}