----------------------------------------------------------------------------------------------------------------------------------------------------------

 泡牛吧!

                                       希望越来越多的光棍能够泡到牛

-----------------------------------------------------------------------------------------------------------------------------------------------------------

程序题(一)
1一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。
a
文件名不包括域名、路径和URL参数,例如http://www.rs.com/n.op/q/rs?id=1中的文件名是rs
b
部分URL可能没有文件名,例如http://www.abc.com/,这类统计为“空文件名”。
c
出现在不同URL中的相同文件名视为同一文件名,例如http://www.ceshi.com/hi.php
ftp://ftp.cdef.com/hi.php为同一文件名

文件内容示例如下:
http://www.test.com/abc/de/fg.php?id=1&url=http://www.test.com/index.html
http://www.ceshi.com/hi.jsp
ftp://ftp.ceshi.com/hi.jsp
http://www.hello.com/cw/hi.jsp?k=8
http://www.hi.com/jk/l.html?id=1&s=a.html
http://www.rs.com/n.op/q/rs?id=1
http://www.abc.com/

package bai;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class Test {
 
public static Set<String> readFile(){
   
     Set<String> set = new HashSet<String>();
     String _str;
     try {
      File f=new File("d:\\test.txt");
   BufferedReader reader = new BufferedReader(new FileReader(f));
   String line;
   while((line=reader.readLine())!=null){
     _str = reads(line);
     if(!_str.equals("")&&_str!=null){
      set.add(_str);
     }
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
     
  } catch (IOException e) {
   e.printStackTrace();
  }
     return set;
 
    }
 
 public static String reads(String str){
     int _len = str.lastIndexOf("?");
     if(_len>0){
      str = str.substring(0,_len);
     }
  return str.substring(str.lastIndexOf("/")+1,str.length()).trim();
 }
 public static void main(String args[]){
    for(String str:Test.readFile()){
     System.out.println(str);
    }
    System.out.println(Test.readFile().size());
   }

}


haohao   2007-05-16 11:13:31 评论:0   阅读:58   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

一切版权属于个人(转载例外)