我試圖解析多個文件並將它們分成一組HashMap中的字段。這是一個樣本文件。解析Java中的文本文件以獲取字段的HashMap
COCONUT OIL CONTRACT TO CHANGE - DUTCH TRADERS
ROTTERDAM, March 18 - Contract terms for trade in coconut
oil are to be changed from long tons to tonnes with effect from
the Aug/Sep contract onwards, Dutch vegetable oil traders said.
Operators have already started to take account of the
expected change and reported at least one trade in tonnes for
Aug/Sept shipment yesterday.
我需要的程序,這個文檔解析爲一個自定義文檔類具有鍵,文件名,文件名稱,地點,日期,作者,內容,類別字段中。
這是我嘗試過的。
public static Document parse(String filename) {
File f = new File(filename);
if (f.isFile()){
String fileId;
if (filename.indexOf(".") > 0) {
fileId = filename.substring(0, filename.lastIndexOf("."));
}
String category = f.getParent();
InputStream in = new FileInputStream(f);
byte buf[] = new byte[1024];
int len = in.read(buf);
while(len > 0){
..........
}
in.close();
}
return null;
}
我很抱歉你試圖在這裏完成? :O – 2014-09-19 19:18:44
那麼,這是一個開始,但很難以相同的方式繼續。如果我是你,我現在不再編寫代碼,首先找出需要採取的高級步驟。把這些步驟寫在一張紙上。 '1。將文件完全讀入字符串。 2.提取文件標題...等等。然後你可以開始一步一步編碼,在每一步之後測試結果。 – biziclop 2014-09-19 19:20:17