参考工具  http://akini.mbnet.fi/java/unicodereader/

Utf8BomRemover 清除bom的方法
    1. package cn.com.do1.component.common.util;
    1. import java.io.*;
    1. import java.nio.charset.Charset;
    1. public class Utf8BomRemover {
    1. /**
    1. * 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃
    1. *
    1. * @param in
    1. * @return
    1. * @throws java.io.IOException
    1. */
    1. public static InputStream getInputStream(InputStream in) throws IOException {
    1. PushbackInputStream testin = new PushbackInputStream(in);
    1. int ch = testin.read();
    1. if (ch != 0xEF) {
    1. testin.unread(ch);
    1. } else if ((ch = testin.read()) != 0xBB) {
    1. testin.unread(ch);
    1. testin.unread(0xef);
    1. } else if ((ch = testin.read()) != 0xBF) {
    1. throw new IOException("错误的UTF-8格式文件");
    1. } else {
    1. // 不需要做,这里是bom头被读完了
    1. // // System.out.println("still exist bom");
    1. }
    1. return testin;
    1. }
    1. /**
    1. * 根据一个文件名,读取完文件,干掉bom头。
    1. *
    1. * @param fileName
    1. * @throws java.io.IOException
    1. */
    1. public static void trimBom(String fileName) throws IOException {
    1. FileInputStream fin = new FileInputStream(fileName);
    1. // 开始写临时文件
    1. InputStream in = getInputStream(fin);
    1. ByteArrayOutputStream bos = new ByteArrayOutputStream();
    1. byte b[] = new byte[4096];
    1. int len = 0;
    1. while (in.available() > 0) {
    1. len = in.read(b, 0, 4096);
    1. // out.write(b, 0, len);
    1. bos.write(b, 0, len);
    1. }
    1. in.close();
    1. fin.close();
    1. bos.close();
    1. // 临时文件写完,开始将临时文件写回本文件。
    1. System.out.println("[" + fileName + "]");
    1. FileOutputStream out = new FileOutputStream(fileName);
    1. out.write(bos.toByteArray());
    1. out.close();
    1. System.out.println("处理文件" + fileName);
    1. }
    1. public static void main(String[] args) throws IOException {
    1. //刪除指定文件夾下(含子文件夾)所有java文件的BOM,若構造器中參數為null則刪除所有文件頭部BOM
    1. new Utf8BomRemover( "java" ).start( "\"F:\\\\flwork\\\\gmmsDGYH\\\\src\\\\com");
    1. }
    1. /**
    1. * 根据一个文件名,读取完文件,干掉bom头2 这里使用了第三方类UnicodeReader
    1. *
    1. * @throws java.io.IOException
    1. */
    1. public void saveFile(String file) throws IOException {
    1. InputStream in= new FileInputStream( file);
    1. BufferedReader bre = null;
    1. OutputStreamWriter pw = null;//定义一个流
    1. CharArrayWriter writer = new CharArrayWriter();
    1. //这一句会读取BOM头
    1. //bre = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    1. //这一句会干掉BOM头
    1. bre = new BufferedReader(new UnicodeReader(in, Charset.defaultCharset().name()));//
    1. String line = bre.readLine();
    1. while(line != null)
    1. {
    1. writer.write(line);
    1. /*
    1. 加上这段代码可以查看更详细的16进制
    1. byte[] allbytes = line.getBytes("UTF-8");
    1. for (int i=0; i < allbytes.length; i++)
    1. {
    1. int tmp = allbytes[i];
    1. String hexString = Integer.toHexString(tmp);
    1. // 1个byte变成16进制的,只需要2位就可以表示了,取后面两位,去掉前面的符号填充
    1. hexString = hexString.substring(hexString.length() -2);
    1. System.out.print(hexString.toUpperCase());
    1. System.out.print(" ");
    1. }*/
    1. line = bre.readLine();
    1. }
    1. writer.flush();
    1. bre.close();
    1. FileWriter f2 = new FileWriter(file);
    1. writer.writeTo(f2);
    1. f2.close();
    1. writer.close();
    1. }
    1. private String extension = null ;
    1. public Utf8BomRemover(String extension) {
    1. super ();
    1. this .extension = extension;
    1. }
    1. /** 啟動對某個文件夾的篩選 */
    1. @SuppressWarnings ( "unchecked" )
    1. public void start(String rootDir) throws IOException {
    1. traverseFolder2(rootDir);
    1. }
    1. public void traverseFolder2(String path) throws IOException {
    1. File file = new File(path);
    1. if (file.exists()) {
    1. File[] files = file.listFiles();
    1. if (files.length == 0) {
    1. System.out.println("文件夹是空的!");
    1. return;
    1. } else {
    1. for (File file2 : files) {
    1. if (file2.isDirectory()) {
    1. System.out.println("文件夹:" + file2.getAbsolutePath());
    1. traverseFolder2(file2.getAbsolutePath());
    1. } else {
    1. remove(file2.getAbsolutePath());
    1. }
    1. }
    1. }
    1. } else {
    1. System.out.println("文件不存在!");
    1. }
    1. }
    1. /** 移除UTF-8的BOM */
    1. private void remove(String path) throws IOException {
    1. saveFile(path);
    1. trimBom(path);
    1. }
    1. }

第二种方法的UnicodeReader 类


    1. package cn.com.do1.component.common.util;
    1. import java.io.IOException;
    1. import java.io.InputStream;
    1. import java.io.InputStreamReader;
    1. import java.io.PushbackInputStream;
    1. import java.io.Reader;
    1. /**
    1. version: 1.1 / 2007-01-25
    1. - changed BOM recognition ordering (longer boms first)
    1. 网络地址:http://koti.mbnet.fi/akini/java/unicodereader/UnicodeReader.java.txt
    1. Original pseudocode : Thomas Weidenfeller
    1. Implementation tweaked: Aki Nieminen
    1. http://www.unicode.org/unicode/faq/utf_bom.html
    1. BOMs:
    1. 00 00 FE FF = UTF-32, big-endian
    1. FF FE 00 00 = UTF-32, little-endian
    1. EF BB BF = UTF-8,
    1. FE FF = UTF-16, big-endian
    1. FF FE = UTF-16, little-endian
    1. Win2k Notepad:
    1. Unicode format = UTF-16LE
    1. ***/
    1. /**
    1. * Generic unicode textreader, which will use BOM mark
    1. * to identify the encoding to be used. If BOM is not found
    1. * then use a given default or system encoding.
    1. */
    1. public class UnicodeReader extends Reader {
    1. PushbackInputStream internalIn;
    1. InputStreamReader internalIn2 = null;
    1. String defaultEnc;
    1. private static final int BOM_SIZE = 4;
    1. /**
    1. *
    1. * @param in inputstream to be read
    1. * @param defaultEnc default encoding if stream does not have
    1. * BOM marker. Give NULL to use system-level default.
    1. */
    1. UnicodeReader(InputStream in, String defaultEnc) {
    1. internalIn = new PushbackInputStream(in, BOM_SIZE);
    1. this.defaultEnc = defaultEnc;
    1. }
    1. public String getDefaultEncoding() {
    1. return defaultEnc;
    1. }
    1. /**
    1. * Get stream encoding or NULL if stream is uninitialized.
    1. * Call init() or read() method to initialize it.
    1. */
    1. public String getEncoding() {
    1. if (internalIn2 == null) return null;
    1. return internalIn2.getEncoding();
    1. }
    1. /**
    1. * Read-ahead four bytes and check for BOM marks. Extra bytes are
    1. * unread back to the stream, only BOM bytes are skipped.
    1. */
    1. protected void init() throws IOException {
    1. if (internalIn2 != null) return;
    1. String encoding;
    1. byte bom[] = new byte[BOM_SIZE];
    1. int n, unread;
    1. n = internalIn.read(bom, 0, bom.length);
    1. if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
    1. (bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
    1. encoding = "UTF-32BE";
    1. unread = n - 4;
    1. } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
    1. (bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
    1. encoding = "UTF-32LE";
    1. unread = n - 4;
    1. } else if ( (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
    1. (bom[2] == (byte)0xBF) ) {
    1. encoding = "UTF-8";
    1. unread = n - 3;
    1. } else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
    1. encoding = "UTF-16BE";
    1. unread = n - 2;
    1. } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
    1. encoding = "UTF-16LE";
    1. unread = n - 2;
    1. } else {
    1. // Unicode BOM mark not found, unread all bytes
    1. encoding = defaultEnc;
    1. unread = n;
    1. }
    1. //System.out.println("read=" + n + ", unread=" + unread);
    1. if (unread > 0) internalIn.unread(bom, (n - unread), unread);
    1. // Use given encoding
    1. if (encoding == null) {
    1. internalIn2 = new InputStreamReader(internalIn);
    1. } else {
    1. internalIn2 = new InputStreamReader(internalIn, encoding);
    1. }
    1. }
    1. public void close() throws IOException {
    1. init();
    1. internalIn2.close();
    1. }
    1. public int read(char[] cbuf, int off, int len) throws IOException {
    1. init();
    1. return internalIn2.read(cbuf, off, len);
    1. }
    1. }




java 清除 bom的更多相关文章

  1. Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50

    Linux下查找包含BOM头的文件和清除BOM头命令 2014-08-16 12:30:50 分类: 系统运维 查找包含BOM头的文件,命令如下: 点击(此处)折叠或打开 grep -r -I -l ...

  2. 批量清除BOM头

    批量清除BOM头 (2012-03-05 13:28:30) 转载▼ 标签: 杂谈   有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题.以下是批量清除bom头的代码,复制代码, ...

  3. Java清除:收尾和垃圾收集

    垃圾收收集器(GC)只知道释放由new关键字分配的内存,所以不知道如何释放对象的"特殊"内存.为了解决这个问题,Java提供了一个名为:finalize()的方法,可为我们的类定义 ...

  4. 2018-03-21 11:34:44 java脚本批量转换java utf-8 bom源码文件为utf-8编码文件

    package com.springbootdubbo; import java.io.*;import java.util.ArrayList;import java.util.List; /** ...

  5. java清除所有微博短链接 Java问题通用解决代码

    java实现微博短链接清除,利用正则,目前只支持微博短链接格式为"http://域名/字母或数字8位以内"的链接格式,现在基本通用 如果链接有多个,返回结果中会有多出的空格,请注意 ...

  6. linux下查找包含BOM头的文件和清除BOM头命令

    查找包含BOM头的文件,命令如下:   grep -r -I -l $'^\xEF\xBB\xBF' ./   这条命令会查找当前目录及子目录下所有包含BOM头的文件,并把文件名在屏幕上输出.   但 ...

  7. 清除BOM头源码

    BOM: Byte Order Mark UTF-8 BOM又叫UTF-8 签名,其实UTF-8 的BOM对UFT-8没有作用,是为了支援UTF-16,UTF-32才加上的BOM,BOM签名的意思就是 ...

  8. Linux 查找bom头文件,清除bom头命令

    1.查找bom头文件 grep -r -I -l $'^\xEF\xBB\xBF' ./ 2.替换bom头文件 find . -type f -exec sed -i 's/\xEF\xBB\xBF/ ...

  9. JAVA输出带BOM的UTF-8编码的文件

    当从http 的response输出CSV文件的时候,设置为utf8的时候默认是不带bom的,可是windows的Excel是使用bom来确认utf8编码的,全部须要把bom写到文件的开头. 微软在 ...

随机推荐

  1. 数学图形(2.8)Viviani曲线

    维维亚尼(Viviani , Vincenzo)意大利数学家.1622年4月5日生于托斯卡纳大区佛罗伦萨:1703年9月22日卒于佛罗伦萨. 这是一个圆柱与一个球相交而生成的曲线. #http://w ...

  2. Jni的Jclass JmethodID JfrieldID的差异

    Jni的Jclass JmethodID JfrieldID 这三者都是java类别的属性,本质上都是指标(Pointer).透过这些指标就能快速调用java类别的函数,或存取对象的属性值.在该类别被 ...

  3. 在css加载完毕后执行后续代码

    最近在写项目的framework,写个JQueryMessageBox的类,以使用jquery ui中的dialog()来显示消息框,为了使方法方便调用,便加入了自动判断页面是否加入了ui.js和ui ...

  4. 高性能WEB开发:DOM编程

    我们知道,DOM是用于操作XML和HTML文档的应用程序接口,用脚本进行DOM操作的代价很昂贵.有个贴切的比喻,把DOM和Javascript(这里指ECMscript)各自想象为一个岛屿,它们之间用 ...

  5. office excel2013如何启用solver选项

    Excel要启用solver很多地方说是要单独安装插件,我认为不同版本可能操作不同.此时office2013已经足够强大,可以通过下面的方法来启用solver 1:在office2013 Excel中 ...

  6. STL栈的应用之表达式求值

    #include<iostream> #include<cstring> #include<cstdio> #include<stack> using ...

  7. C#应用视频教程3.2 Halcon软件测试

    Halcon是图像处理比较牛逼的一个软件(德国人的东西,做的非常强大,里面集成的算法很多,可能你一辈子研究这个也做不过他),然而牛逼归牛逼,康耐视,基恩士也很牛逼,但是日本人的东西一般太过封闭,屌丝用 ...

  8. Python网络爬虫 - 1. 准备工作

    1. 安装Beautiful Soup 下载地址 http://www.crummy.com/software/BeautifulSoup/bs4/download/4.4/ 解压后,进入根目录 控制 ...

  9. Java程序实现密钥库的维护

    1 Java程序列出密钥库所有条目 import java.util.*; import java.io.*; import java.security.*; public class ShowAli ...

  10. Python——极限编程

    很cool的名字,极限编程(Extreme Programming,简写XP)是编程的一种流行趋势: (1)首先是对目标进行计划: (2)然后将测试用例集合编写为一种框架: (3)之后才编写实际的代码 ...