java對於目錄下檔案的單詞查詢操作程式碼實現
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了java對於目錄下檔案的單詞查詢操作程式碼實現,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
寫入檔案的目錄。程式碼通過找目錄下的檔案,進行相關函式的操作。如果目錄下面包含子目錄。程式碼設有呼叫遞迴的方法,在尋找子目錄下的檔案
在進行相關的函式操作。函式主要是按使用者輸入的個數要求輸出檔案中出現次數最多的前幾位字母。
package com.keshangone; //將想要輸出的資料寫入新的檔案裡面 import java.util.*; import java.io.*; import java.util.Scanner; public class zongword { static public void gongneng(String file1) throws IOException { System.out.println("想要輸出前幾個的單詞"); Scanner sc1=new Scanner(System.in); int www=sc1.nextInt(); int ww=0; int ha=0; Map<String,Integer> map=new HashMap<>();//通過map儲存對映,和陣列類似 File file=new File(file1); FileReader fr=new FileReader(file); try { BufferedReader bd=new BufferedReader(fr); String wen=null; while((wen=bd.readLine())!=null)//讀入一行資料 { String []word=wen.split(" ");//通過空格將整行資料分成多個字串並儲存在字串數組裡 ha+=word.length; for(int i=0;i<word.length;i++) { if(word[i].equals(" ")) { continue; } if(map.containsKey(word[i].toLowerCase()))//檢查集合中是否有這個元素 { Integer a=map.get(word[i].toLowerCase()); a++; map.put(word[i].toLowerCase(),a);//為他出現的次數加一 } else map.put(word[i].toLowerCase(),1);//如果從未出現過就將他的values賦值為一 } map.put(" ",0); } fr.close(); }catch (Exception e)//程式的異常處理 { e.printStackTrace(); } File file2=new File("D:\\新建資料夾 (6)\\wen1.txt"); if(!file2.exists()) { try { file2.createNewFile(); System.out.println("資料輸出的指向檔案不存在已經為您新建一個以保留執行結果請繼續操作"); } catch(Exception e) { e.printStackTrace(); } } FileWriter fw=new FileWriter(file2); BufferedWriter bw=new BufferedWriter(fw); Integer max1; String max2=""; for(int b=1;b<=ha;b++) { max1=0; max2=null; //找出出現次數最多的單詞 Set<String> set=map.keySet();//構建map集合所有key物件集合 Iterator <String> it=set.iterator();//建立集合迭代器 while(it.hasNext()) { String key1=it.next(); Integer a2=map.get(key1); if(a2>max1) { max1=a2; max2=key1; } } //主要是為了判斷是否會出現出現次數相同的單詞 Set<String> set2=map.keySet();//構建map集合所有key物件集合 Iterator <String> it2=set2.iterator();//建立集合迭代器 while(it2.hasNext()) { String key2=it2.next(); Integer a3=map.get(key2); if(a3==max1) { Set<String> set3=map.keySet();//構建map集合所有key物件集合 Iterator <String> it3=set2.iterator();//建立集合迭代器 while(it3.hasNext()) { String key4=it3.next(); Integer a4=map.get(key4); if(a4==a3) { if(key2.compareTo(key4)<0) { key2=key4; } } } if(max1==0) { break; } bw.write("出現次數排在第 "+b+" 位的單詞是 "+key2+" 出現次數是 "+a3); bw.newLine(); System.out.print("單詞 "+key2+"次數"+a3+" "); if(ww==www) { System.out.println(); break; } ww++; map.put(key2,0);//輸出之後讓他的values變為0,防止阻礙後面的判斷 } } if(ww==www) { System.out.println(); break; } } System.out.println("相關資料已經全部寫入相應的資料夾裡(在螢幕上也進行了顯示)"); bw.close(); fw.close(); } static private void getDirectory(File file) throws IOException { File flist[] = file.listFiles(); if (flist == null || flist.length == 0) { return; } for (File f : flist) { if (f.isDirectory()) { //這裡將列出所有的資料夾 getDirectory(f); //getDirectory(f); } else { System.out.println("file==>" + f.getAbsolutePath()); gongneng( f.getAbsolutePath()); System.out.println(); } } } static int q1=0; static Scanner sc=new Scanner(System.in); public static void main(String[] args)throws IOException { String path="D:\\新建資料夾 (10)"; File fm=new File(path); getDirectory(fm); } }
主要是關於檔案目錄中尋找檔案,以及遞迴呼叫的相關操作。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。