C#课后小试8
Stream(数据流)
命名空间:System.IO
我们看一下文件的读写
1.二进制文件
1 //所读取的文件 2 Stream inputStream=File.OpenRead( 3 @"C:\test\source\test1.cs"); 4 //要写入的文件 5 Stream outputStream=File.Openwrite( 6 @"C:\test\source\test1.bak"); 7 8 //创建一个保存字节的缓存 9 byte [] buffer=new Byte[SizeBuffer]; 10 int byteRead; 11 12 whil(byteRead=inputStream.Read(buffer,0,SizeBuff))>0) 13 { 14 outputStream.Write(buffer,0,byteRead); 15 }
2.缓存数据流
1 //创建 2 3 Stream inputStream=File.OpenRead( 4 5 @"C:\test\source\folder3.cs"); 6 7 Stream outputStream=File.OpenWrite( 8 9 @"C:\test\source\folder3.bak"); 10 11 //添加缓存数据流 12 13 BufferedStream bufferedInput= 14 15 new BufferedStream(inputStream); 16 17 BufferedStream bufferedOutput= 18 19 new BufferedStream(OutputStream); 20 21 byte[] buffer=new Byte[SizeBuffer]; 22 23 int bytesRead; 24 25 while((bytesRead= 26 27 bufferedInput.Read(buffer,0,SizeBuff))>0) 28 29 { 30 31 bufferedOutput.Write(buffer,0,bytesRead); 32 33 }