import java.util.*;
import java.io.*;
public class ProURL{
private HashMap map;
public ProURL(){
this.map=new HashMap();
}
private String parseURL(String fPath){
String rFile="rurl.txt";
try{
FileReader fr=new FileReader(fPath);
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter(rFile);
BufferedWriter bw=new BufferedWriter(fw);
String str;
while((str=br.readLine())!=null){
StringBuffer tmp;
String pre;
String[] array=str.split("/");
int len=array.length;
pre=array[0]+"//";
try{
for(int i=2;i<len-1;i++){
tmp.append(array[i]);
String strTmp=tmp.toString();
strTmp=strTmp+"\n";
bw.write(strTmp);
pre=strTmp;
}
bw.flush();
bw.close();
fw.close();
}catch(Exception ex){
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return rFile;
}
private void addURL(String url){
if(map.containsKey(url)){
int c=((Integer)map.get(url)).intValue()+1;
map.put(url,new Integer(c));
}else{
map.put(url,1);
}
}
public void process(String fPath){
String rPath=parseURL(fPath);
FileReader fr=new FileReader(rPath);
BufferedReader br=new BufferedReader(fr);
String url;
while((url=br.readLine())!=null){
this.addURL(url);
}
fr.close();
br.close();
}
public void main(String[] args){
ProURL pro=new ProURL();
String fPath="url.txt";
pro.process(fPath);
}
}
shell命令:
awk -F/ '{pre=$1"//";i=3; while(i<NF){str=pre$i"/"; count[str]++;i++;}} END{for(url in count){ print url,count[url];}}' urlfile
看来shell还是强大的。