ex-21-06

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

TXT
50
字号
// Example 21-06: Reading and writing to a text file

namespace Programming_CSharp
{
   using System;
   using System.IO;

   class Tester
   {
      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()
      {
         // open a file
         FileInfo theSourceFile = new FileInfo(
            @"C:\test\source\test.cs");

         // create a text reader for that file
         StreamReader reader = theSourceFile.OpenText();

         // create a text writer to the new file
         StreamWriter writer = new StreamWriter(
            @"C:\test\source\test.bak",false);

         // create a text variable to hold each line
         string text;

         // walk the file and read every line
         // writing both to the console
         // and to the file
         do
         {
            text = reader.ReadLine();
            writer.WriteLine(text);
            Console.WriteLine(text);
         } while (text != null); 

         // tidy up
         reader.Close();
         writer.Close();
      }
   }
}

⌨️ 快捷键说明

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