• C# 实践 6 System.IO.BinaryReader / System.IO.BinaryWriter


    参考:

    https://docs.microsoft.com/en-us/dotnet/api/system.io.binaryreader?view=netframework-4.8

    https://docs.microsoft.com/en-us/dotnet/api/system.io.binarywriter?view=netframework-4.8

    读 / 写二进制文件的类。

    参考代码:

    using System;
    using System.IO;
    
    class ConsoleApplication
    {
        const string fileName = "AppSettings.dat";
    
        static void Main()
        {
            WriteDefaultValues();
            DisplayValues();
        }
    
        public static void WriteDefaultValues()
        {
            using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
            {
                writer.Write(1.250F);
                writer.Write(@"c:Temp");
                writer.Write(10);
                writer.Write(true);
            }
        }
    
        public static void DisplayValues()
        {
            float aspectRatio;
            string tempDirectory;
            int autoSaveTime;
            bool showStatusBar;
    
            if (File.Exists(fileName))
            {
                using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
                {
                    aspectRatio = reader.ReadSingle();
                    tempDirectory = reader.ReadString();
                    autoSaveTime = reader.ReadInt32();
                    showStatusBar = reader.ReadBoolean();
                }
    
                Console.WriteLine("Aspect ratio set to: " + aspectRatio);
                Console.WriteLine("Temp directory is: " + tempDirectory);
                Console.WriteLine("Auto save time set to: " + autoSaveTime);
                Console.WriteLine("Show status bar: " + showStatusBar);
            }
        }
    }
    

      

  • 相关阅读:
    Java数据结构概述·14
    Java之自定义异常·13
    idea spirng项目jsp页面乱码
    HashMap和LinkedHashMap的区别
    jar包导入仓库中
    后台接口接受前端参数的时候使用包装类和基本类型接受
    转 为什么程序员怕改需求?
    thymeleaf常用标签
    linux查看端口占用情况
    查询linux硬件配置
  • 原文地址:https://www.cnblogs.com/alexYuin/p/12404414.html
Copyright © 2020-2023  润新知