java實(shí)現(xiàn)文件搜索主要使用file類和正則表達(dá)式,如下示例:
銅仁ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
package?com.kiritor.util;
import?java.io.File;
import?java.io.FileFilter;
import?java.util.Arrays;
import?java.util.Collections;
import?java.util.List;
/**
*?文件的相關(guān)操作類
*?
*?@author?Kiritor
*/
public?class?FileOperation?{
private?static?String?contentPath;
private?static?String?filePath;
private?static?File[]?fileList?=?null;//?保存文件列表,過濾掉目錄
public?FileOperation()?{
}
/**?構(gòu)造函數(shù)的參數(shù)是一個(gè)目錄?*/
public?FileOperation(String?path)?{
File?file?=?new?File(path);
if?(file.isDirectory())
this.contentPath?=?path;
else
this.filePath?=?path;
}
/**獲取文件列表*/
public?static?File[]?getFiles()?{
if?(contentPath?==?null)?{
File?file?=?new?File(filePath);
fileList?=?new?File[1];
fileList[0]?=?file;
return?fileList;
}
fileList?=?new?File(contentPath).listFiles(new?FileFilter()?{
/**使用過濾器過濾掉目錄*/
@Override
public?boolean?accept(File?pathname)?{
if(pathname.isDirectory())
{
return?false;
}else
return?true;
}
});
return?fileList;
}
/**?對(duì)當(dāng)前目錄下的所有文件進(jìn)行排序?*/
public?static?File[]?sort()?{
getFiles();
Arrays.sort(fileList,?new?FileComparator());
return?fileList;
}
public?static?void?tree(File?f,?int?level)?{
String?preStr?=?"";
for(int?i=0;?ilevel;?i++)?{
preStr?+=?"????";
}
File[]?childs?=?f.listFiles();
//返回一個(gè)抽象路徑名數(shù)組,這些路徑名表示此抽象路徑名表示的目錄中的文件。
for(int?i=0;?ichilds.length;?i++)?{
System.out.println(preStr?+?childs[i].getName());
if(childs[i].isDirectory())?{
tree(childs[i],?level?+?1);
}
}
}
//?提供一個(gè)"比較器"
static?class?FileComparator?implements?java.util.ComparatorFile?{
@Override
public?int?compare(File?o1,?File?o2)?{
//?按照文件名的字典順序進(jìn)行比較
return?o1.getName().compareTo(o2.getName());
}
}
}
import?java.io.*;
public?class?FileDemo{
public?static?void?main(String[]?args)throws?Exception{
//第一個(gè)參數(shù)是文件路徑,第二個(gè)參數(shù)是要搜索的文件擴(kuò)展名
getFile("D:\\JavaDemo",".txt");
}
private?static?void?getFile(String?pathName,?final?String?endsWith)throws?Exception{
File?file?=?new?File(pathName);
if(!file.exists())
throw?new?RuntimeException("文件不存在,你檢索個(gè)P呀。");
file.listFiles(new?FileFilter(){
public?boolean?accept(File?file){
if(file.getName().endsWith(endsWith)){
System.out.println(file.getName());
return?true;
}else
return?false;
}
});
}
}
java編寫的GUI 怎么實(shí)現(xiàn)查找功能:
package communitys.Connect;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class dxdsy extends JFrame implements ActionListener{
private JButton button = new JButton("搜索");
private JTextField textfile = new JPasswordField("請(qǐng)輸入文件名稱······");
public dxdsy()
{
this.setLayout(null);
this.setBounds(200,200, 500,500);
textfile.setBounds(1, 1, 100,20);
button.setBounds(1, 25, 80,80);
this.add(button);
this.add(textfile);
button.addActionListener(this);//添加事件監(jiān)聽
this.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void actionPerformed(ActionEvent e) {
String sql = "select * from tablename where 條件 like '%"+textfile.getText()+"%'";
try {
Class.forName("驅(qū)動(dòng)字符");
Connection conn = DriverManager.getConnection("驅(qū)動(dòng)字符");
Statement sta = conn.createStatement();
ResultSet rs = sta.executeQuery(sql);
//這個(gè)rs集合當(dāng)中就是想要的數(shù)據(jù)
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}