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 閱讀(1859)
評論(0) 編輯 收藏