ch8_01.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 63 行

CS
63
字号
using System;
using System.IO;

class CH8_1
{
   public static void CreateTextFile( string name )
   {
        try
	{
           FileStream fs = new 
               FileStream(name, FileMode.Create, FileAccess.Write);
           StreamWriter sw = new StreamWriter(fs);
  	   sw.WriteLine("Test1");
           sw.Write("This is");
           sw.Write(" ");
           sw.Write("a test\n");
           sw.Close();
	}
	catch ( Exception e )
	{
	   Console.WriteLine("Exception in CreateTextFile: {0}", e );
	}
   }
   
   public static void CreateBinaryFile( string name )
   {
        try
	{
           FileStream fs = new 
               FileStream(name, FileMode.Create, FileAccess.Write);
           BinaryWriter bw = new BinaryWriter(fs);
	
           int x = 10;
  	   double y = 20.45;
           string s = "This is a test";
	
           bw.Write(x);
           bw.Write(y);
           bw.Write(s);
	
           bw.Close();
	}
	catch ( Exception e )
	{
	   Console.WriteLine("Exception in CreateBinaryFile: {0}", e );
	}
   }
   
   public static void Main( string[] args )
   {
      if ( args.Length < 2 )
      {
         Console.WriteLine("Usage CH8_1 text-file-name binary-file-name");
         return;
      }
	 
      CreateTextFile( args[0] );
      CreateBinaryFile( args[1] );
   }
}

   

⌨️ 快捷键说明

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