主要是要讀檔並使用二階的treeMap方式來存放
1. test.log
C01,01,A,10
C02,04,A,15
C03,05,A,10
C02,06,A,20
C01,09,A,3
C01,10,C,7
C05,12,D,10
C05,13,A,0
C06,19,C,10
2. ReadLogFileTest.java
1. test.log
C01,01,A,10
C02,04,A,15
C03,05,A,10
C02,06,A,20
C01,09,A,3
C01,10,C,7
C05,12,D,10
C05,13,A,0
C06,19,C,10
2. ReadLogFileTest.java
import java.io.*;
import java.util.*;
class LogData
{
private String cardNo;
private String seqNo;
private String txnType;
private String txnAmount;
public LogData(String [] sAry )
{
this.setCardNo(sAry[0]);
this.setSeqNo(sAry[1]);
this.setTxnType(sAry[2]);
this.setTxnAmount(sAry[3]);
}
public void setCardNo( String sCardNo )
{
cardNo = sCardNo;
}
public String getCardNo()
{
return cardNo;
}
public void setSeqNo( String sSeqNo )
{
seqNo = sSeqNo;
}
public String getSeqNo()
{
return seqNo;
}
public void setTxnType( String sTxnType )
{
txnType = sTxnType;
}
public String getTxnType()
{
return txnType;
}
public void setTxnAmount( String sTxnAmount )
{
txnAmount = sTxnAmount;
}
public String getTxnAmount()
{
return txnAmount;
}
public String print(){
StringBuffer sb = new StringBuffer();
sb.append("Card No : " + cardNo + "\n");
sb.append("Seq No : " + seqNo + "\n");
sb.append("Txn Type : " + txnType + "\n");
sb.append("txnAmount : " + txnAmount + "\n");
return sb.toString();
}
}
public class ReadLogFileTest {
public static void main(String [] args ) throws IOException
{
FileReader fr = new FileReader("test.log");
BufferedReader br = new BufferedReader(fr);
TreeMap <String , TreeMap > map = new TreeMap<String , TreeMap>() ;
TreeSet <String&gt;set = new TreeSet<String&gt;();
try
{
String s = new String();
while( (s = br.readLine()) != null )
{
System.out.println(s);
String [] strData = s.split(",");
LogData lg = new LogData(strData);
//System.out.println(lg.toString());
TreeMap <String , LogData> subMap = new TreeMap<String , LogData>() ;
subMap.put( lg.getSeqNo() , lg );
//System.out.println(subMap);
if(map.containsKey(lg.getCardNo())){
TreeMap tm = map.get(lg.getCardNo());
tm.put(lg.getSeqNo() ,lg);
map.put(lg.getCardNo() , tm );
}else{
map.put(lg.getCardNo() , subMap );
set.add(lg.getCardNo());
}
}
//System.out.println("TreeMap Size:"+map.size());
Iterator it =set.iterator();
while(it.hasNext()){
String k = (String)it.next();
TreeMap <String , LogData> tm = map.get(k) ;
//System.out.println("["+k+"]"+tm);
System.out.println("["+k+"]");
Collection collection = tm.values();
Iterator iterator = collection.iterator();
while(iterator.hasNext()) {
LogData lg =(LogData)iterator.next();
//System.out.println(iterator.next());
System.out.println(lg.print());
}
}
}catch( Exception e){
System.out.println("Exception : " + e );
}finally{
br.close();
}
}
}