C # · complete solution for BOM header

Time:2021-7-24
Reading time |0.46 minutes word count |798.4 characters
primary coverage |1. Introduction & background
“C # · complete solution for BOM header”
Author | SCscHero Writing time | 2021/3/13 PM10:38
Article type |Series Degree of completion |To be improved
motto Every great cause has a trivial beginning.

1、 Introduction & background    Completion: 100%


a) Dealing with problems

  1. How to use c# to write files with / without BOM headers?
  2. How to use c# to determine whether a document has a BOM header?

b) Application scenario

  1. Process bom header files.
  2. When reading files, avoid exceptions caused by BOM headers.

c) Preparatory knowledge

  1. Need to understandWhat is the BOM header?

2、 Use c# to write files with / without BOM headers?    Completion: 100%


Use the parameterized constructor to determine whether to use the BOM header. The following code:

var utf8WithoutBom = new System.Text.UTF8Encoding(false);// Use the constructor Boolean parameter to specify whether the BOM header is included. For example, false is not included.
            var utf8WithBom = new System.Text.UTF8Encoding(false);// Use the constructor Boolean parameter to specify whether the BOM header is included. For example, true is included.
            using (var sink = new StreamWriter("WithoutBom.txt", false, utf8WithoutBom))
            {
                sink.WriteLine("Hello,SCscHero");
            }
            using (var sink = new StreamWriter("WithBom.txt", false, utf8WithBom))
            {
                sink.WriteLine("Hello,SCscHero");
            }

3、 When reading files, avoid exceptions caused by BOM headers.    Completion: 100%


According to the characteristics of BOM header, we can use the method of judging byte stream. The following code:

var fileByte = System.IO.File.ReadAllBytes(@"TestBom.txt");
            var isBom = isBomHeader(fileByte);
            Console. Writeline ("contains BOM header: {0}", isbom);

The code for judging whether it is a BOM header can be encapsulated into a common method, as follows:

public static bool isBomHeader(byte[] bs)
        {
            int len = bs.Length;
            if (len >= 3 && bs[0] == 0xEF && bs[1] == 0xBB && bs[2] == 0xBF)
            {
                return true;
            }
            return false;
        }

4、 Statements and references    Completion: 100%


Original blog post, please do not reprint without permission.

If you are helpful, you are welcome to like, collect and pay attention. If you have any questions, please comment! If you need to contact the blogger, you can write scschero directly.

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]