C#/Winform

[C#] BinaryWriter, BinaryReader 사용하기

kjun.kr 2022. 4. 1. 23:47
728x90
728x170
using System;
using System.IO;

namespace App
{
    class Program
    {
        static void Main(string[] args)
        {
            // Binary Data 쓰기
            BinaryWriter binaryWriter = new BinaryWriter(new FileStream("BinaryData.dat", FileMode.Create));

            binaryWriter.Write(5);
            binaryWriter.Write("String");
            binaryWriter.Write(1.78f);
            binaryWriter.Write("String2");
            binaryWriter.Write(0.025d);

            binaryWriter.Close();

            // Binary Data 읽기
            BinaryReader binaryReader = new BinaryReader(new FileStream("BinaryData.dat", FileMode.Open));

            Console.WriteLine("File Size: {0} bytes", binaryReader.BaseStream.Length);
            Console.WriteLine("{0}", binaryReader.ReadInt32());
            Console.WriteLine("{0}", binaryReader.ReadString());
            Console.WriteLine("{0}", binaryReader.ReadSingle());
            Console.WriteLine("{0}", binaryReader.ReadString());
            Console.WriteLine("{0}", binaryReader.ReadDouble());

            binaryReader.Close();
        }
    }
}

 

결과

728x90
그리드형