首页 > 编程语言 > 详细

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

时间:2021-04-17 22:55:15      阅读:32      评论:0      收藏:0      [点我收藏+]
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();
        }
    }
}

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

原文:https://blog.51cto.com/geym2008/2714097

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!