运维开发网

Java处理UTF-8文件的BOM头部

运维开发网 https://www.qedev.com 2021-04-18 10:04 出处:51CTO 作者:快乐领读
Java处理UTF-8文件的BOM头部BOM——ByteOrderMark,就是字节序标记。基本概念在UCS编码中有一个叫做”ZEROWIDTHNO-BREAKSPACE“的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符”ZEROWIDTHNO-BREAKSPACE“。如果接收者收到FEFF,就表明这个字节流是

Java处理UTF-8文件的BOM头部

BOM——Byte Order Mark,就是字节序标记。

基本概念

  • UCS 编码中有一个叫做”ZERO WIDTH NO-BREAK SPACE“的字符,它的编码是FEFF。而FFFEUCS中是不存在的字符,所以不应该出现在实际传输中。
  • UCS规范建议我们在传输字节流前,先传输 字符”ZERO WIDTH NO-BREAK SPACE“。
  • 如果接收者收到FEFF,就表明这个字节流是大字节序的;如果收到FFFE,就表明这个字节流是小字节序的。因此字符”ZERO WIDTH NO-BREAK SPACE“又被称作BOM

UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符”ZERO WIDTH NO-BREAK SPACE“的UTF-8编码EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。

这个BOM头部对于UTF-8来说不是必须的,并且我建议最好不用有这个头部,以避免可能的兼容性问题。

下面就来看看怎么用java来处理UTF-8的BOM头部

增加BOM到UTF-8文件

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class AddBomToUtf8File {

  public static void main(String[] args) throws IOException {

      Path path = Paths.get("/home/file.txt");
      writeBomFile(path, "billy");

  }

  private static void writeBomFile(Path path, String content) {
        // Java 8 default UTF-8
        try (BufferedWriter bw = Files.newBufferedWriter(path)) {
            bw.write("\ufeff");
            bw.write(content);
            bw.newLine();
            bw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

在Java 8 之前可以用下面的方法:

              new OutputStreamWriter(
                      new FileOutputStream(path.toFile())
                      , StandardCharsets.UTF_8))) {
          bw.write("\ufeff");
          bw.write(content);
          bw.newLine();
          bw.write(content);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

或者可以用 PrintWriter 和OutputStreamWriter

      try (PrintWriter pw = new PrintWriter(
              new OutputStreamWriter(
                      new FileOutputStream(path.toFile()), StandardCharsets.UTF_8))) {
          //pw.write("\ufeff");
          pw.write(0xfeff); // alternative, codepoint
          pw.write(content);
          pw.write(System.lineSeparator());
          pw.write(content);

      } catch (IOException e) {
          e.printStackTrace();
      }
  }

又或者,这样:

  private static void writeBomFile4(Path path, String content) {
      try (FileOutputStream fos = new FileOutputStream(path.toFile())) {

          byte[] BOM = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};

          fos.write(BOM);
          fos.write(content.getBytes(StandardCharsets.UTF_8));
          fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
          fos.write(content.getBytes(StandardCharsets.UTF_8));

      } catch (IOException e) {
          e.printStackTrace();
      }
  }

检查文件是否包含BOM头部

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CheckBom {

  public static void main(String[] args) throws IOException {

      Path path = Paths.get("/home/file.txt");

      if(isContainBOM(path)){
          System.out.println("Found BOM!");
      }else{
          System.out.println("No BOM.");
      }

  }

  private static boolean isContainBOM(Path path) throws IOException {

      if(Files.notExists(path)){
          throw new IllegalArgumentException("Path: " + path + " does not exists!");
      }

      boolean result = false;

      byte[] bom = new byte[3];
      try(InputStream is = new FileInputStream(path.toFile())){

          // read first 3 bytes of a file.
          is.read(bom);

          // BOM encoded as ef bb bf
          String content = new String(Hex.encodeHex(bom));
          if ("efbbbf".equalsIgnoreCase(content)) {
              result = true;
          }

      }

      return result;
  }

}

上面的代码需要一个依赖:

  <dependency>
      <groupId>commons-codec</groupId>
      <artifactId>commons-codec</artifactId>
      <version>1.14</version>
  </dependency>

移除UTF-8文件的BOM头部

通常,我建议不要用这个BOM,不然处理不好产生什么乱码就麻烦了。


import org.apache.commons.codec.binary.Hex;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class RemoveBomFromUtf8File {

  public static void main(String[] args) throws IOException {

      Path path = Paths.get("/home/file.txt");
      writeBomFile(path, "billy");
      removeBom(path);

  }

  private static void writeBomFile(Path path, String content) {
      // Java 8 default UTF-8
      try (BufferedWriter bw = Files.newBufferedWriter(path)) {
          bw.write("\ufeff");
          bw.write(content);
          bw.newLine();
          bw.write(content);
      } catch (IOException e) {
          e.printStackTrace();
      }
  }

  private static boolean isContainBOM(Path path) throws IOException {

      if (Files.notExists(path)) {
          throw new IllegalArgumentException("Path: " + path + " does not exists!");
      }

      boolean result = false;

      byte[] bom = new byte[3];
      try (InputStream is = new FileInputStream(path.toFile())) {

          // read 3 bytes of a file.
          is.read(bom);

          // BOM encoded as ef bb bf
          String content = new String(Hex.encodeHex(bom));
          if ("efbbbf".equalsIgnoreCase(content)) {
              result = true;
          }

      }

      return result;
  }

  private static void removeBom(Path path) throws IOException {

      if (isContainBOM(path)) {

          byte[] bytes = Files.readAllBytes(path);

          ByteBuffer bb = ByteBuffer.wrap(bytes);

          System.out.println("Found BOM!");

          byte[] bom = new byte[3];
          // get the first 3 bytes
          bb.get(bom, 0, bom.length);

          // remaining
          byte[] contentAfterFirst3Bytes = new byte[bytes.length - 3];
          bb.get(contentAfterFirst3Bytes, 0, contentAfterFirst3Bytes.length);

          System.out.println("Remove the first 3 bytes, and overwrite the file!");

          // override the same path
          Files.write(path, contentAfterFirst3Bytes);

      } else {
          System.out.println("This file doesn't contains UTF-8 BOM!");
      }

  }

}

复制UTF-8文件并追加BOM

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class CopyAndAddBomToXmlFile {

    public static void main(String[] args) {
        Path src = Paths.get("src/main/resources/staff.xml");
        Path dest = Paths.get("src/main/resources/staff-bom.xml");
        writeBomFile(src, dest);
    }

    private static void writeBomFile(Path src, Path dest) {

        try (FileOutputStream fos = new FileOutputStream(dest.toFile())) {

            byte[] BOM = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
            // add BOM
            fos.write(BOM);

            // BOM + src to fos
            Files.copy(src, fos);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0

精彩评论

暂无评论...
验证码 换一张
取 消