2007年百度招聘在线笔试真题解答$百度$

时间:2017-10-21 笔试题目 我要投稿

  问题:



  一、 一个文本文件有多行,每行为一个URL。请编写代码,统计出URL中的文件名及出现次数。

  a) 文件名不包括域名、路径和URL参数,例如https://www.rs.com/n.op/q/rs?id=1中的文件名是rs。

  b) 部分URL可能没有文件名,例如https://www.abc.com/,这类统计为“空文件名”。

  c) 出现在不同URL中的相同文件名视为同一文件名,例如https://www.ceshi.com/hi.php和ftp://ftp.cdef.com/hi.php为同一文件名

  文件内容示例如下:

  https://www.test.com/abc/de/fg.php?id=1
import java.io.*;import java.util.*; /** * @author tzy * 在j2sdk1.4.2下测
试通过 */public class FileNameStat{ private String srcPath;//要统计
的文件路径 private Map statMap;//用于统计的map public
FileNameStat(String srcPath) { this.srcPath=srcPath
; statMap=new TreeMap(); } /*获得要统
计的URL的文件名*/ public String getFileName(String urlString)
{ URL url=null; String filePath=null;
String fileName=null; try {
url=new URL(urlString); filePath=url
.getPath(); int index=0; if (
(index=filePath.lastIndexOf("/"))!=-1) {
fileName=filePath.substring(index 1);
} else {
fileName=""; } }
catch(MalformedURLException e) { }
return fileName; } /*统计指定文件名的
个数*/ public void stat(String filename) { Integer
count=null; if(statMap.get(filename)!=null)
{ count=(Integer)statMap.get(filename);
count=new Integer(count.intValue() 1); }
else { count=new Integer(1);
} statMap.put(filename,count);
} /*统计的主方法*/ public void start() throws FileNotFoundException
,IOException { BufferedReader bfin=new BufferedReader
(new FileReader(this.srcPath)); String temp=null;
while((temp=bfin.readLine())!=null) {
stat(getFileName(temp)); } } /*
输出统计结果*/ public void result() { Iterator
it=statMap.entrySet().iterator(); while(it.hasNext())
{ Map.Entry entry=(Map.Entry)(it.next());
System.out.println((entry.getKey().equals("")?"空文
件名":entry.getKey()) "的个数是" entry.getValue()); }

} public static void main(String[] args) throws

2007年百度招聘在线笔试真题解答$百度$相关推荐
热门推荐