Last active
December 11, 2015 18:18
-
-
Save weirhp/4640082 to your computer and use it in GitHub Desktop.
http://wendal.net/266.html 使用了v5兽的的代码 判断并移除UTF-8的BOM头
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.chinaedu.projects; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.PushbackInputStream; | |
/** | |
* 判断并移除UTF-8的BOM头 | |
* @author houpo@chinaedu.net | |
* | |
*/ | |
public class RemoveFileUTF8BOM { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
String path = "E:\\work\\projects\\g3\\G3\\code\\trunk\\venuskeeper"; | |
File file = new File(path); | |
removeDir(file); | |
} | |
public static void removeDir(File file) { | |
if (file.isFile() && file.getName().endsWith(".java")) { | |
removeFileBom(file); | |
} else if (file.isDirectory()) { | |
File[] files = file.listFiles(); | |
for (File file2 : files) { | |
removeDir(file2); | |
} | |
} | |
} | |
public static void removeFileBom(File file) { | |
try { | |
String path = file.getCanonicalPath(); | |
InputStream in = utf8filte(new FileInputStream(file)); | |
File tmpFile = File.createTempFile("utf8", ".tmp"); | |
FileOutputStream out = new FileOutputStream(tmpFile); | |
byte[] buffer = new byte[2048]; | |
int count = 0; | |
while ((count = in.read(buffer)) > 0) { | |
out.write(buffer, 0, count); | |
} | |
in.close(); | |
out.close(); | |
file.delete(); | |
in = new FileInputStream(tmpFile); | |
out = new FileOutputStream(path); | |
while ((count = in.read(buffer)) > 0) { | |
out.write(buffer, 0, count); | |
} | |
in.close(); | |
out.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static final byte[] UTF_BOM = new byte[] { (byte) 0xEF, (byte) 0xBB, | |
(byte) 0xBF }; | |
/** | |
* 判断并移除UTF-8的BOM头 | |
*/ | |
public static InputStream utf8filte(InputStream in) { | |
try { | |
PushbackInputStream pis = new PushbackInputStream(in, 3); | |
byte[] header = new byte[3]; | |
pis.read(header, 0, 3); | |
if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] | |
|| header[2] != UTF_BOM[2]) { | |
pis.unread(header, 0, 3); | |
} | |
return pis; | |
} catch (IOException e) { | |
} | |
return in; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment