`
frank1998819
  • 浏览: 733325 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类

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

    博客分类:
  • Java
阅读更多

(原作者地址)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<String> 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<String> 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");

}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics