import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class UtfTool
{
/**
* ant 编译之后的result文件,注意要编译提示错误的文件名要在同一行
* 可以设置命令提示窗口的缓冲区大小实现
* @param resultFileName
*/
public static Set getFileNamesFromCompileResult(String resultFileName)
throws java.io.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;
}
/**
* 读取流中前面的字符,看是否有bom,如果有bom,将bom头先读掉丢弃
* @param in
* @return
* @throws 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 IOException
*/
public static void trimBom(String fileName) throws IOException
{
FileInputStream fin = new FileInputStream(fileName);
//开始写临时文件
InputStream in = getInputStream(fin);
String tmpFileName = fileName + ".tmp";
FileOutputStream out = new FileOutputStream(tmpFileName);
byte b[] = new byte[4096];
int len = 0;
while (in.available() > 0)
{
len = in.read(b, 0, 4096);
out.write(b, 0, len);
}
in.close();
fin.close();
out.close();
//临时文件写完,开始将临时文件写回本文件。
in = new FileInputStream(tmpFileName);
System.out.println("[" + fileName + "]");
out = new FileOutputStream(fileName);
while (in.available() > 0)
{
len = in.read(b, 0, 4096);
out.write(b, 0, len);
}
in.close();
out.close();
}
/**
* 根据ant编译错误来去除bom
* @param resultFile
* @throws 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
{
trimBomByCompileResult("c:/result3.txt");
///List<String> list = getFileNamesFromCompileResult(resultFile);
// InputStream in = new FileInputStream(
// "D:/cc/SSB_SRC_DEV_lijinglin2/TP_SSB/SSB_CJ203_Component/ProductCode/com/zte/ssb/component/access/implementation/dao/ImplementationDAO.java");
// getInputStream2(in);
// in.close();
}
}
文章来源:
http://stocknewbie.bokee.com/viewdiary.22061282.html
posted on 2009-05-01 10:52
huohuo 阅读(1852)
评论(0) 编辑 收藏