批量删除UTF-8文件头BOM
2018-08-20 11:08:58
1176次阅读
0个评论
批量移除UTF-8的BOM,其实质就是删除带有BOM信息的UTF-8文件的前三字节
先引入apache commons-io.jar包
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import org.apache.commons.io.DirectoryWalker;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
@SuppressWarnings("rawtypes")
public class Utf8BomRemover extends DirectoryWalker {
public static void main(String[] args) throws IOException {
//删除指定文件夹下(含子文件夹)所有java文件的BOM,若构造器中参数为null则删除所有文件头部BOM
new Utf8BomRemover("java").start(new File("E:/workspace/Test/src"));
}
private String extension = null;
public Utf8BomRemover(String extension) {
super();
this.extension = extension;
}
/** 启动对某个文件夹的筛选 */
@SuppressWarnings("unchecked")
public void start(File rootDir) throws IOException {
walk(rootDir, null);
}
protected void handleFile(File file, int depth, Collection results) throws IOException {
if (extension == null
|| extension.equalsIgnoreCase(FilenameUtils.getExtension(file.toString()))) {
//调用具体业务逻辑,其实这里不仅可以实现删除BOM,还可以做很多想干的事情。
remove(file);
}
}
/** 移除UTF-8的BOM */
private void remove(File file) throws IOException {
byte[] bs = FileUtils.readFileToByteArray(file);
if (bs[0] == -17 && bs[1] == -69 && bs[2] == -65) {
byte[] nbs = new byte[bs.length - 3];
System.arraycopy(bs, 3, nbs, 0, nbs.length);
FileUtils.writeByteArrayToFile(file, nbs);
System.out.println("Remove BOM: " + file);
}
}
}
00