java對于目錄下文件的單詞查找操作代碼實現(xiàn)
這篇文章主要介紹了java對于目錄下文件的單詞查找操作代碼實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
寫入文件的目錄。代碼通過找目錄下的文件,進行相關(guān)函數(shù)的操作。如果目錄下面包含子目錄。代碼設(shè)有調(diào)用遞歸的方法,在尋找子目錄下的文件
在進行相關(guān)的函數(shù)操作。函數(shù)主要是按用戶輸入的個數(shù)要求輸出文件中出現(xiàn)次數(shù)最多的前幾位字母。
package com.keshangone;
//將想要輸出的數(shù)據(jù)寫入新的文件里面
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保存映射,和數(shù)組類似
File file=new File(file1);
FileReader fr=new FileReader(file);
try
{
BufferedReader bd=new BufferedReader(fr);
String wen=null;
while((wen=bd.readLine())!=null)//讀入一行數(shù)據(jù)
{
String []word=wen.split(" ");//通過空格將整行數(shù)據(jù)分成多個字符串并保存在字符串數(shù)組里
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);//為他出現(xiàn)的次數(shù)加一
}
else
map.put(word[i].toLowerCase(),1);//如果從未出現(xiàn)過就將他的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("數(shù)據(jù)輸出的指向文件不存在已經(jīng)為您新建一個以保留運行結(jié)果請繼續(xù)操作");
}
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;
//找出出現(xiàn)次數(shù)最多的單詞
Set<String> set=map.keySet();//構(gòu)建map集合所有key對象集合
Iterator <String> it=set.iterator();//創(chuàng)建集合迭代器
while(it.hasNext())
{
String key1=it.next();
Integer a2=map.get(key1);
if(a2>max1)
{
max1=a2;
max2=key1;
}
}
//主要是為了判斷是否會出現(xiàn)出現(xiàn)次數(shù)相同的單詞
Set<String> set2=map.keySet();//構(gòu)建map集合所有key對象集合
Iterator <String> it2=set2.iterator();//創(chuàng)建集合迭代器
while(it2.hasNext())
{
String key2=it2.next();
Integer a3=map.get(key2);
if(a3==max1)
{
Set<String> set3=map.keySet();//構(gòu)建map集合所有key對象集合
Iterator <String> it3=set2.iterator();//創(chuàng)建集合迭代器
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("出現(xiàn)次數(shù)排在第 "+b+" 位的單詞是 "+key2+" 出現(xiàn)次數(shù)是 "+a3);
bw.newLine();
System.out.print("單詞 "+key2+"次數(shù)"+a3+" ");
if(ww==www)
{
System.out.println();
break;
}
ww++;
map.put(key2,0);//輸出之后讓他的values變?yōu)?,防止阻礙后面的判斷
}
}
if(ww==www)
{
System.out.println();
break;
}
}
System.out.println("相關(guān)數(shù)據(jù)已經(jīng)全部寫入相應(yīng)的文件夾里(在屏幕上也進行了顯示)");
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);
}
}
主要是關(guān)于文件目錄中尋找文件,以及遞歸調(diào)用的相關(guān)操作。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot詳細講解異步任務(wù)如何獲取HttpServletRequest
在使用框架日常開發(fā)中需要在controller中進行一些異步操作減少請求時間,但是發(fā)現(xiàn)在使用@Anysc注解后會出現(xiàn)Request對象無法獲取的情況,本文就此情況給出完整的解決方案2022-04-04
spring data簡化jpa開發(fā)_動力節(jié)點Java學院整理
這篇文章主要介紹了spring data簡化jpa開發(fā),本文主要講述 Spring Data JPA,希望能夠深入學習 Spring 和 JPA,有興趣的可以了解下2017-09-09
JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié)
轉(zhuǎn)發(fā)和重定向是JavaWeb中常用的兩種頁面跳轉(zhuǎn)方式,它們在實現(xiàn)上有一些區(qū)別,本文主要介紹了JavaWeb中轉(zhuǎn)發(fā)與重定向的區(qū)別小結(jié),具有一定的參考價值,感興趣的可以了解一下2023-10-10

