NEWS

[C#] Hướng dẫn đọc, ghi file nhị phân trong lập trình csharp

[C#] Hướng dẫn đọc, ghi file nhị phân trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 10130 08:20:41, 21/02/2019DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn cách đọc và ghi file nhị phân (Binary File) trong lập trình C#.

Vậy tập tin nhị phân là gì?

Tập tin nhị phân là một tập tin không phải là tập tin văn bản, tập tin được lưu trữ ở dữ liệu thô gồm các số 0 và 1.

Trong C#, có cung cấp cho chúng ta 2 lớp BinaryReaderBinaryWriter, giúp chúng ta làm việc với tập tin nhị phân một cách dễ dàng.

Hình ảnh mẫu của một tập tin nhị phân:

binary_file

Souce code đọc và ghi file nhị phân trong C#:

using System;
using System.IO;
 
namespace BinaryFileApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            BinaryWriter bw;
            BinaryReader br;
            int i = 13;
            double d = 2.56317;
            bool b = true;
            string s = "Hello World !!";
 
            //create the file
            try
            {
                bw = new BinaryWriter(new FileStream("mydata", FileMode.Create));
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message + "
 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 + "
 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 + "
 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 + "
 Cannot read from file.");
                return;
            }
            //close the file
            br.Close();
 
            Console.ReadKey();
        }
    }
}

Kết quả:

Reading_Writing_Binary_File

Thanks for watching!

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn đọc, ghi file nhị phân trong lập trình csharp
Đăng bởi: Thảo Meo - Lượt xem: 10130 08:20:41, 21/02/2019DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.