百度java笔一试题(含答案).docVIP

  • 5
  • 0
  • 约9.48千字
  • 约 9页
  • 2016-12-22 发布于湖南
  • 举报
更多面试题, 百度面试笔试题 解答答案 专家回答: 第一题 ?? 简评 ?? 百度的主要业务是搜索,搜索的基本原理如下   1.编写爬虫程序到互联网上抓取网页海量的网页。   2.将抓取来的网页通过抽取,以一定的格式保存在能快速检索的文件系统中。   3.把用户输入的字符串进行拆分成关键字去文件系统中查询并返回结果。   由以上3点可见,字符串的分析,抽取在搜索引擎中的地位是何等重要。   因此,百度的笔试面试题中,出现这样的题就变得理所当然了。   以下是该题的java实现,代码如下: 程序代码 程序代码 import .*; 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(Value()+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.

文档评论(0)

1亿VIP精品文档

相关文档