善忘技术夹 Logo
善忘技术夹
技术文章 · 1939 阅读

去掉UTF8 BOM格式,转换成UTF8格式

public static void utf8bom2Utf8(String dir) throws IOException{

File file = new File(dir);

if(file.isDirectory()){

File[] files = file.listFiles();

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

utf8bom2Utf8(files[i].getAbsolutePath());

}

} else{

FileInputStream fis = new FileInputStream(file);

long size = fis.getChannel().size();

byte[] b = new byte[3];

fis.read(b, 0, 3);

String str = "";

for (byte c : b) {

str += Integer. toHexString(c&0xff);

}

if(“efbbbf” .equals(str)){

System. out.println(file.getAbsolutePath());

byte[] newbytes = new byte[( int)size-3];

fis.read(newbytes,0,( int)size-3);

fis.close();

FileOutputStream fos = new FileOutputStream(file);

fos.write(newbytes);

fos.flush();

fos.close();

}

}

}