Skip to content

Commit

Permalink
add: 移除文件UTF8 BOM头的小工具
Browse files Browse the repository at this point in the history
Signed-off-by: Wendal Chen <[email protected]>
  • Loading branch information
wendal committed Sep 5, 2012
1 parent 5c00f49 commit 6a53e3a
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tools/org/nutz/tools/UTF8_BOM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.nutz.tools;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

import org.nutz.lang.Files;
import org.nutz.lang.Streams;
import org.nutz.lang.util.Disks;
import org.nutz.lang.util.FileVisitor;

public class UTF8_BOM {

public static void main(String[] args) {
final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
final byte[] bom = new byte[3];
Disks.visitFile(new File("."), new FileVisitor() {

public void visit(File file) {
try {
FileInputStream fis = new FileInputStream(file);
fis.read(bom);
if (bom[0] == UTF_BOM[0] && bom[1] == UTF_BOM[1] && bom[2] == UTF_BOM[2]) {
System.out.println("Found BOM --> " + file);
byte[] data = Streams.readBytes(fis);
fis.close();
Files.write(file, data);
System.out.println("Fixed");
}
}
catch (Throwable e) {
e.printStackTrace();
}
}
}, new FileFilter() {

public boolean accept(File pathname) {
if (pathname.isDirectory())
return true;
return pathname.getName().endsWith(".java") && pathname.length() > 3;
}
});
}
}

0 comments on commit 6a53e3a

Please sign in to comment.