using System;using System.IO;namespace BinaryFileApplication {class Program {static void Main(string[] args) {
编程学习网为您整理以下代码实例,主要实现:C# I/O二进制文件,希望可以帮到各位朋友。
using System;
using System.IO;
namespace BinaryfileApplication {
   class Program {
      static voID Main(string[] args) {
         BinaryWriter bw;
         BinaryReader br;
         int i = 25;
         double d = 3.14157;
         bool b = true;
         string s = "I am happy";
         //create the file
         try {
            bw = new BinaryWriter(new fileStream("mydata", fileMode.Create));
         } catch (IOException e) {
            Console.Writeline(e.Message + "\n Cannot create file.");
            return;
         }
         //writing into the file
         try {
            bw.Write(i);
            bw.Write(d);
            bw.Write(b);
            bw.Write(s);
         } catch (IOException e) {
            Console.Writeline(e.Message + "\n Cannot write to file.");
            return;
         }
         bw.Close();
         //reading from the file
         try {
            br = new BinaryReader(new fileStream("mydata", fileMode.Open));
         } catch (IOException e) {
            Console.Writeline(e.Message + "\n Cannot open file.");
            return;
         }
         try {
            i = br.ReadInt32();
            Console.Writeline("Integer data: {0}", i);
            d = br.ReadDouble();
            Console.Writeline("Double data: {0}", d);
            b = br.ReadBoolean();
            Console.Writeline("Boolean data: {0}", b);
            s = br.ReadString();
            Console.Writeline("String data: {0}", s);
         } catch (IOException e) {
            Console.Writeline(e.Message + "\n Cannot read from file.");
            return;
         }
         br.Close();
         Console.ReadKey();
      }
   }
}
				 沃梦达教程
				
			本文标题为:C# I/O二进制文件
 
				
         
 
            
        
             猜你喜欢
        
	     - C#检查字符串是否包含特殊字符 1970-01-01
- C#基本语法 1970-01-01
- C#将十进制转换为八进制数 1970-01-01
- C#嵌套switch语句 1970-01-01
- C#使用string.concat来连接字符串 1970-01-01
- C#抽象和虚拟类 1970-01-01
- C#调用方法示例2 1970-01-01
- C#使用递归来计算阶乘 1970-01-01
- C# break语句 1970-01-01
- C#主线程 1970-01-01
