通过循环累加查出出现次数最多的单词
以空格做间隔
文件导入避免重复输入
public class test{ /* * 读取指定路径下的文件名和目录名 */ public void getFileList() throws IOException { System.out.println("请输入路径"); Scanner scan=new Scanner(System.in); String a=scan.next(); File file = new File(a); File[] fileList = file.listFiles(); for (int i1 = 0; i1 < fileList.length; i1++) { if (fileList[i1].isFile()) { String fileName = fileList[i1].getName(); if(fileName.indexOf(".txt")!=-1) { Word word=new Word(); //单词的链头 Word lian,xin; String str=""; String S=a+"\"+fileName; System.out.println(S); FileReader f=new FileReader(S); char[] c=new char[1]; int b=0; boolean exist=false; while((b=f.read(c))!=-1) { if(String.valueOf(c).equals(" ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(" ")||String.valueOf(c).equals(",")||String.valueOf(c).equals(".")||String.valueOf(c).equals(""")||String.valueOf(c).equals("'")) { lian=word; while(lian!=null) { if(lian.value.equalsIgnoreCase(str)) { lian.geshu++;exist=true;break; } else { lian=lian.next; } } if(exist==false) { xin=new Word(str,1); xin.next=word.next; word.next=xin; str=""; } else { exist=false; str=""; } } else //单词 { str+=String.valueOf(c); } } System.out.println("请输入您想查询的前几个出现此处最多的单词"); int N=scan.nextInt(); for(int i=1;i<=N;i++) { xin=new Word("",0); lian=word.next; while(lian!=null) { if(lian.geshu>xin.geshu) { xin=lian; } lian=lian.next; } System.out.println("第"+i+"个 :"+xin.value+"个数:"+xin.geshu); lian=word; while(lian.next!=null) { if(lian.next.value.equalsIgnoreCase(xin.value)) { lian.next=lian.next.next; break; } lian=lian.next; } } } System.out.println("文件:" + fileName); } } } }