文件与流相关code

FileStream方式写入文件

FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);

fs.Write(fileData, 0, fileData.Length); //写入流  

fs.Flush(); //清空缓冲区 

fs.Close(); //关闭流

fs.Dispose();

 

BinaryWriter方式写文件

FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);

using (BinaryWriter bw = new BinaryWriter(fs))

{

    bw.Write(fileData); //写入流                    

    bw.Flush();//清空缓冲区                    

    bw.Close(); //关闭流

}

fs.Close();

fs.Dispose();

 

FileStream fs1 = new FileStream(@"x1.doc", FileMode.Open);

FileStream fs2 = new FileStream(@"x2.doc", FileMode.Create);

byte[] data = new byte[1024];

//创建两个缓冲流,与两个文件流相关联

BufferedStream bs1 = new BufferedStream(fs1);

BufferedStream bs2 = new BufferedStream(fs2);

while (fs1.Read(data, 0, data.Length) > 0)

{

    fs2.Write(data, 0, data.Length);

    fs2.Flush();

}

fs1.Close();

fs2.Close();

 

FileStream fs = new FileStream(op.FileName, FileMode.Open);

//把文件读取到字节数组

byte[] data = new byte[fs.Length];

fs.Read(data, 0, data.Length);

fs.Close();

 

Stream s = fileUpload.PostedFile.InputStream;

//文件临时储存Byte数组

Byte[] fileData = new Byte[fileUpload.PostedFile.ContentLength];

s.Read(fileData, 0, fileUpload.PostedFile.ContentLength);

s.Close();

 

FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write); 

StreamWriter sw = new StreamWriter(fs);

sw.Write(this.textBox1.Text);

sw.Flush();//清空缓冲区

sw.Close();//关闭流

fs.Close();  

 

//获得字节数组  

byte[] data = new System.Text.UTF8Encoding().GetBytes(this.textBox1.Text);

你可能感兴趣的:(code)