java中utf-8含义,除开java代码中的UTF-8 BOM标识(转)

去除java代码中的UTF-8 BOM标识(转)

(原作者地址)http://hi.baidu.com/893625/blog/item/c5dbf9d158080c359b502715.html

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.PushbackInputStream;

import java.util.ArrayList;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

public class BOMRemover {

/**

* ant 编译之后的result文件,注意要编译提示错误的文件名要在同一行 可以设置命令提示窗口的缓冲区大小实现

*

* @param resultFileName

*/

public static Set getFileNamesFromCompileResult(String resultFileName)

throws java.io.IOException {

Set set = new HashSet();

BufferedReader reader = new BufferedReader(new FileReader(

resultFileName));

String start = "[javac] ";

int startLen = start.length();

String end = ".java:";

int endLen = end.length();

String errMsg = "\\65279";

while (reader.ready()) {

String line = reader.readLine();

int indexStart = line.indexOf(start);

if (line.indexOf(errMsg) == -1) {

continue;

}

if (indexStart != -1) {

int indexEnd = line.indexOf(end);

if (indexEnd != -1) {

String name = line.substring(indexStart + startLen,

indexEnd + endLen - 1);

set.add(name.trim());

}

}

}

return set;

}

public static void DealSrcFiles(String path) {

if (path.charAt(path.length() - 1) != '\\') {

path += '\\';

}

File file = new File(path);

if (!file.exists()) {

System.out.println("Error: Path not Existed! Please Check it out!");

return;

}

String[] filelist = file.list();

for (int i = 0; i < filelist.length; i++) {

File temp = new File(path + filelist[i]);

if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {

DealSrcFiles(path + filelist[i]);

} else {

if (filelist[i].endsWith(".java")) {

try {

trimBom(path + filelist[i]);

} catch (Exception eee) {

}

}

}

}

}

/**

* 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃

*

* @param in

* @return

* @throws IOException

*/

public static InputStream getInputStream(InputStream in) throws IOException {

PushbackInputStream testin = new PushbackInputStream(in);

int ch = testin.read();

if (ch != 0xEF) {

testin.unread(ch);

} else if ((ch = testin.read()) != 0xBB) {

testin.unread(ch);

testin.unread(0xef);

} else if ((ch = testin.read()) != 0xBF) {

throw new IOException("错误的UTF-8格式文件");

} else {

// 不需要做,这里是bom头被读完了

// // System.out.println("still exist bom");

}

return testin;

}

/**

* 根据一个文件名,读取完文件,干掉bom头。

*

* @param fileName

* @throws IOException

*/

public static void trimBom(String fileName) throws IOException {

FileInputStream fin = new FileInputStream(fileName);

// 开始写临时文件

InputStream in = getInputStream(fin);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

byte b[] = new byte[4096];

int len = 0;

while (in.available() > 0) {

len = in.read(b, 0, 4096);

//            out.write(b, 0, len);

bos.write(b, 0, len);

}

in.close();

fin.close();

bos.close();

// 临时文件写完,开始将临时文件写回本文件。

System.out.println("[" + fileName + "]");

FileOutputStream  out = new FileOutputStream(fileName);

out.write(bos.toByteArray());

out.close();

System.out.println("处理文件"+fileName);

}

/**

* 根据ant编译错误来去除bom

*

* @param resultFile

* @throws IOException

*/

static void trimBomByCompileResult(String resultFile) throws IOException {

Set set = getFileNamesFromCompileResult(resultFile);

for (String fName : set) {

trimBom(fName);

}

}

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

//        if(args.length==0){

//            DealSrcFiles(System.getProperty("user.dir"));

//        }

//        else{

//            DealSrcFiles(args[0]);

//        }

//直接扫描所有的java文件

DealSrcFiles("F:/workspace/proj/src");

//另一种调用方式,只扫描ant报错的文件,只需把报错文件另存为即可

//trimBomByCompileResult("d:/bom.txt");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值