在某些时候解析XML文件时,会出现root element is missing类似错误,一般为文件编码问题,之前碰到一个问题客户提供一份XML文件,进行测试,一直提示该错误,用文本编辑软件查看,没有问题,但是新建xml文件,将其中内容复制进去,再次测试正常,发现该文件为带BOM的UTF-8。查找将BOM去除的java代码,目前只找到一个方法,并测试有用。
/**
*
* @param resultFileName
*/
public static Set getFileNamesFromCompileResult(String resultFileName)
throws IOException {
Set<String> set = new HashSet();
BufferedReader reader = new BufferedReader(new FileReader(
resultFileName));
String start = "[javac] ";
int startLen = start.length();
String end = ".java:";
int endLen = end.length();
String errMsg = "\\65279";
while (reader.ready()) {
String line = reader.readLine();
int indexStart = line.indexOf(start);
if (line.indexOf(errMsg) == -1) {
continue;
}
if (indexStart != -1) {
int indexEnd = line.indexOf(end);
if (indexEnd != -1) {
String name = line.substring(indexStart + startLen,
indexEnd + endLen - 1);
set.add(name.trim());
}
}
}
return set;
}
public static void DealSrcFiles(String path) {
if (path.charAt(path.length() - 1) != '\\') {
path += '\\';
}
File file = new File(path);
if (!file.exists()) {
System.out.println("Error: Path not Existed! Please Check it out!");
return;
}
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File temp = new File(path + filelist[i]);
if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {
DealSrcFiles(path + filelist[i]);
} else {
if (filelist[i].endsWith(".java")) {
try {
trimBom(path + filelist[i]);
} catch (Exception eee) {
}
}
}
}
}
/**
* 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃
*
* @param in
* @return
* @throws java.io.IOException
*/
public static InputStream getInputStream(InputStream in) throws IOException {
PushbackInputStream testin = new PushbackInputStream(in);
int ch = testin.read();
if (ch != 0xEF) {
testin.unread(ch);
} else if ((ch = testin.read()) != 0xBB) {
testin.unread(ch);
testin.unread(0xef);
} else if ((ch = testin.read()) != 0xBF) {
throw new IOException("错误的UTF-8格式文件");
} else {
// 不需要做,这里是bom头被读完了
// // System.out.println("still exist bom");
}
return testin;
}
/**
* 根据一个文件名,读取完文件,干掉bom头。
*
* @param fileName
* @throws java.io.IOException
*/
public static void trimBom(String fileName) throws IOException {
FileInputStream fin = new FileInputStream(fileName);
// 开始写临时文件
InputStream in = getInputStream(fin);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte b[] = new byte[4096];
int len = 0;
while (in.available() > 0) {
len = in.read(b, 0, 4096);
// out.write(b, 0, len);
bos.write(b, 0, len);
}
in.close();
fin.close();
bos.close();
// 临时文件写完,开始将临时文件写回本文件。
System.out.println("[" + fileName + "]");
FileOutputStream out = new FileOutputStream(fileName);
out.write(bos.toByteArray());
out.close();
System.out.println("处理文件" + fileName);
}
/**
* 根据ant编译错误来去除bom
*
* @param resultFile
* @throws java.io.IOException
*/
static void trimBomByCompileResult(String resultFile) throws IOException {
Set<String> set = getFileNamesFromCompileResult(resultFile);
for (String fName : set) {
trimBom(fName);
}
}
public static void main(String[] args) throws IOException {
DealSrcFiles("D:/ttt/p");
}
以下是该作者方法链接