import org.apache.commons.lang.ArrayUtils;
public class EncodeRepairTool {
public static final byte[] bPre = "EFBBBF".getBytes();
private int i = 0;
/**
* @param args
*/
public static void main(String[] args) {
String path = "D:\\eclipse-dev-3.3\\workspace";
File file = new File(path);
EncodeRepairTool scanner = new EncodeRepairTool();
scanner.scanFolder(file);
}
public void scanFolder(File file) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
scanFolder(files[i]);
}
} else if (file.getName().endsWith(".java")) {
removePreCode(file);
}
}
private void removePreCode(File file) {
try {
FileInputStream fis = new FileInputStream(file);
int size = fis.available();
if (size < 24) {
return;
}
i ++ ;
byte[] bs = new byte[size];
fis.read(bs);
byte[] tbs = ArrayUtils.subarray(bs, 0, 3);
byte[] tbs1 = new byte[] { new Integer(0xEF).byteValue(),
new Integer(0xBB).byteValue(),
new Integer(0xBF).byteValue() };
boolean bol = false;
if (tbs[0] == tbs1[0] && tbs[1] == tbs1[1] && tbs[2] == tbs1[2]) {
bol = true;
}
fis.close();
if (!bol) {
System.out.println(" " + i + " : " + file.getName());
tbs = bs;
}
else {
System.out.println("**" + i + " : " + file.getName());
tbs = ArrayUtils.subarray(bs, 3, size);
}
InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(tbs), "UTF-8");
BufferedReader br = new BufferedReader(reader);
StringBuffer buffer = new StringBuffer();
String s = br.readLine();
while (s != null) {
buffer.append(s);
buffer.append("\n");
s = br.readLine();
}
reader.close();
byte[] nbs = buffer.toString().getBytes("GBK");
FileOutputStream fos = new FileOutputStream(file);
fos.write(nbs);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO 鑷姩鐢熸垚 catch 鍧?br />
e.printStackTrace();
} catch (IOException e) {
// TODO 鑷姩鐢熸垚 catch 鍧?br />
e.printStackTrace();
}
}
}