ex-21-04

来自「Programming Csharp Source Code(代码) Prog」· 代码 · 共 48 行

TXT
48
字号
// Example 21-04: Implementing a binary read and write to a file

namespace Programming_CSharp
{
   using System;
   using System.IO;

   class Tester
   {
      const int SizeBuff = 1024;

      public static void Main()
      {
         // make an instance and run it
         Tester t = new Tester();
         t.Run();
      }
        
      // Set it running with a directory name
      private void Run()
      {
         // the file to read from
         Stream inputStream = File.OpenRead(
            @"C:\test\source\test1.cs");

         // the file to write to
         Stream outputStream = File.OpenWrite(
            @"C:\test\source\test1.bak");

         // create a buffer to hold the bytes 
         byte[] buffer = new Byte[SizeBuff];
         int bytesRead; 

         // while the read method returns bytes
         // keep writing them to the output stream
         while ( (bytesRead = 
            inputStream.Read(buffer,0,SizeBuff)) > 0 )
         {
            outputStream.Write(buffer,0,bytesRead);
         }

         // tidy up before exiting
         inputStream.Close();
         outputStream.Close();
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?