Java實(shí)現(xiàn)文件檢索系統(tǒng)的示例代碼
示例代碼
package Demo;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Scanner;
import java.lang.Exception;
import java.io.IOException;
public class Demo8_1 {
public static void main(String[] args) {
while(true) {
System.out.println("1、按關(guān)鍵字檢索文件");
System.out.println("2、按后綴名找出文件");
System.out.println("3、退出");
System.out.println("請(qǐng)選擇你的操作");
Scanner sr = new Scanner(System.in);
int choose = sr.nextInt();
if(choose==1) {
searchByKey();
}else if(choose==2) {
searchBySuffix();
}else if(choose==3) {
exit();
}
}
}
//退出
public static void exit() {
System.out.println("你已經(jīng)退出系統(tǒng),感謝使用!");
System.exit(0);
}
//按照關(guān)鍵字查找
public static void searchByKey() {
Scanner sr = new Scanner(System.in);
System.out.println("請(qǐng)輸入要檢索文件的位置:");
String dirPath = sr.next();
File dir = new File(dirPath);
File[]files = dir.listFiles();
String[] fileNames = new String[files.length];
System.out.println("請(qǐng)輸入檢索文件關(guān)鍵字");
String mainName = sr.next();
//(dir.getName()+"/"+mainName);
Filter filter = new Filter();
for(int i=0;i<files.length;i++) {
fileNames[i] = files[i].getName();
if(filter.acceptLikeName(fileNames[i],mainName)) {
System.out.println(fileNames[i]);
}
}
}
//按照后綴名查找
public static void searchBySuffix() {
Scanner sr = new Scanner(System.in);
System.out.println("請(qǐng)輸入要檢索文件的位置:");
String dirPath = sr.next();
File dir = new File(dirPath);
File[]files = dir.listFiles();
String[] fileNames = new String[files.length];
Filter filter = new Filter();
System.out.println("請(qǐng)輸入后綴名");
String endName = sr.next();
for(int i=0;i<files.length;i++) {
fileNames[i] = files[i].getName();
if(filter.acceptEndName(dir,fileNames[i],endName)) {
System.out.println("后綴名為"+endName+"的有:"+fileNames[i]);
}
}
}
}
//繼承類FilenameFileter
class Filter implements FilenameFilter {
public boolean accept(File dir,String name) {
return true;
}
public boolean acceptLikeName(String name,String mainName) {
if(name.contains(mainName)) {
return true;
}
return false;
}
public boolean acceptEndName(File dir,String name,String endName) {
File file = new File(dir,name);
if(file.isFile()&&name.endsWith(endName)) {
return true;
}
return false;
}
}
效果圖展示


到此這篇關(guān)于Java實(shí)現(xiàn)文件檢索系統(tǒng)的示例代碼的文章就介紹到這了,更多相關(guān)Java文件檢索系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Data JPA中的動(dòng)態(tài)查詢實(shí)例
本篇文章主要介紹了詳解Spring Data JPA中的動(dòng)態(tài)查詢。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Spring Data JPA開啟批量更新時(shí)樂觀鎖失效問題的解決方法
樂觀鎖的基本思想是,認(rèn)為在大多數(shù)情況下,數(shù)據(jù)訪問不會(huì)導(dǎo)致沖突,因此,樂觀鎖允許多個(gè)事務(wù)同時(shí)讀取和修改相同的數(shù)據(jù),而不進(jìn)行顯式的鎖定,本文給大家介紹了Spring Data JPA開啟批量更新時(shí)樂觀鎖失效問題的解決方法,需要的朋友可以參考下2024-07-07
SpringBoot使用RestTemplate如何通過http請(qǐng)求將文件下載到本地
文章介紹了如何通過編寫代碼批量下載文件,解決了沒有提供批量下載接口的問題,首先篩選出需要下載的文件ID,然后通過后端代碼發(fā)起HTTP請(qǐng)求,將下載的資源寫入本地文件中,總結(jié)了實(shí)現(xiàn)方式和注意事項(xiàng),希望能為類似任務(wù)提供參考2025-02-02
Springboot shiro認(rèn)證授權(quán)實(shí)現(xiàn)原理及實(shí)例
這篇文章主要介紹了Springboot shiro認(rèn)證授權(quán)實(shí)現(xiàn)原理及實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
spring項(xiàng)目實(shí)現(xiàn)單元測(cè)試過程解析
這篇文章主要介紹了spring項(xiàng)目實(shí)現(xiàn)單元測(cè)試過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
Java基礎(chǔ)之重載(Overload)與重寫(Override)詳解
這篇文章主要介紹了Java基礎(chǔ)之重載(Overload)與重寫(Override)詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
從零開始學(xué)Java之關(guān)系運(yùn)算符
今天帶大家復(fù)習(xí)Java關(guān)系運(yùn)算符,文中對(duì)Java運(yùn)算符相關(guān)知識(shí)作了詳細(xì)總結(jié),對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們很有幫助,需要的朋友可以參考下2021-08-08

