新聞中心
java搜索文件的代碼怎么寫,返回文件的路徑?求教
你是搜文件名,還是搜文件內(nèi)容?要是搜文件內(nèi)容可就麻煩了,有可能的話你看看Java的一個開源庫Lucene。
莊河網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),莊河網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為莊河上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的莊河做網(wǎng)站的公司定做!
要是簡單的搜文件名包含的字符串,大致應(yīng)該涉及到文件樹的遍歷算法,最多用一些簡單的正則表達式來匹配文件名,一般用遞歸可以實現(xiàn)任意級目錄樹的搜索。
給你個簡單的版本吧:
package?test.tool;
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileReader;
import?java.io.IOException;
import?java.util.regex.Matcher;
import?java.util.regex.Pattern;
public?class?FindFile?{
private?String?fileName?=?"";
private?String?dir?=?"";
private?Matcher?m?=?null;
private?int?count?=?0;
public?FindFile()?throws?IOException?{
String?f?=?FindFile.class.getResource("findfile.properties").getFile();
BufferedReader?read?=?new?BufferedReader(new?FileReader(f));
dir?=?read.readLine().trim();
fileName?=?read.readLine().trim();
Pattern?p?=?Pattern.compile(fileName);
m?=?p.matcher("");
}
public?void?find()?{
File?root?=?new?File(dir);
for?(File?f?:?root.listFiles())?{
if?(f.isDirectory())?{
dir?=?f.getAbsolutePath();
find();
}?else?{
m.reset(f.getName());
if?(m.find())?{
count++;
System.out.println(f.getAbsolutePath());
}
}
}
}
public?static?void?main(String[]?args)?{
try?{
FindFile?ff?=?new?FindFile();
ff.find();
System.out.println("\n共找到文件數(shù)目:"?+?ff.count);
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
里面用到的findfile.properties,舉個例子:
F:\download
vod.*.exe
運行效果如下:
F:\download\firefox\vodplayer.exe
F:\download\ie\vodplayer.exe
共找到文件數(shù)目:2
Java 用戶界面設(shè)計 求界面代碼
一: 首先弄清題目的意思
A.需要的主要組件列表:
1. ?創(chuàng)建一個窗口,窗口標(biāo)題叫Information
2. ?3個標(biāo)簽, 用于顯示文字 Name Number Class
3. ?3個文本框, 用于填寫信息
4. ?1個按鈕, ?文字是確認
5. ?1個文本域
B.業(yè)務(wù)邏輯
1. 當(dāng)點擊按鈕確認的時候, 把 文本框的信息顯示到文本域
C.設(shè)計的主要技術(shù)
JLabel , JButton, JTextField ...等, 都是swing的組件 , ?所以應(yīng)該使用swing進行創(chuàng)建
二: ?確定使用的布局
swing雖然重寫了大部分的組件, 但是布局, 依舊沿襲awt技術(shù)
分析圖片上的布局:
至少有2種方法可以實現(xiàn),?
方法一: 絕對布局 , 優(yōu)點: ?配合可視化GUI拖曳, 可以完美的實現(xiàn)圖上的組件的位置
但是缺點也是致命的, 不同的操作系統(tǒng)平臺下, 可能會出現(xiàn)位置的移動,
只適合開發(fā)平臺, 移植效果差 . ?所以不推薦使用
方法二: 靈活的表格布局, 配合流式布局 , 所有操作系統(tǒng)下,顯示效果都比較統(tǒng)一.?
三: 效果圖
四: 參考代碼
import?java.awt.*;
import?java.awt.event.*;
import?javax.swing.*;
public?class?FrameDemo?extends?JFrame?{
//申明需要的組件
private?final?JTextField?jtf1,jtf2,jtf3;
private?final?JTextArea?jta;
public?FrameDemo()?{
setTitle("Information");//設(shè)置窗口標(biāo)題
setSize(320,?360);//設(shè)置窗口大小
setLocationRelativeTo(null);//設(shè)置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//設(shè)置關(guān)閉時退出虛擬機
getContentPane().setLayout(new?FlowLayout());//設(shè)置窗口布局為流式布局
JPanel?jp?=?new?JPanel(new?GridLayout(4,?2));//設(shè)置jp面板為表格布局4行2列
//第一行
JPanel?jp01?=?new?JPanel();
JLabel?jl1?=?new?JLabel("Name:");
jp01.add(jl1);
JPanel?jp1?=?new?JPanel();
jtf1?=?new?JTextField(8);
jp1.add(jtf1);
//第二行
JPanel?jp02?=?new?JPanel();
JLabel?jl2?=?new?JLabel("Number:");
jp02.add(jl2);
JPanel?jp2?=?new?JPanel();
jtf2?=?new?JTextField(8);
jp2.add(jtf2);
//第三行
JPanel?jp03?=?new?JPanel();
JLabel?jl3?=?new?JLabel("Class:");
jp03.add(jl3);
JPanel?jp3?=?new?JPanel();
jtf3?=?new?JTextField(8);
jp3.add(jtf3);
//第四行
JPanel?jp04?=?new?JPanel();
JLabel?jl4?=?new?JLabel("");
jp04.add(jl4);
JPanel?jp4?=?new?JPanel();
JButton?jb?=?new?JButton("確認");
jp4.add(jb);
jp.add(jp01);
jp.add(jp1);
jp.add(jp02);
jp.add(jp2);
jp.add(jp03);
jp.add(jp3);
jp.add(jp04);
jp.add(jp4);
getContentPane().add(jp);
jta?=?new?JTextArea();
jta.setColumns(20);//設(shè)置文本域的大小
jta.setEditable(false);//設(shè)置文本域不可編輯
jta.setBackground(jp.getBackground());//設(shè)置文本域的背景色和面板一樣
getContentPane().add(jta);
jb.addActionListener(new?ActionListener()?{//給按鈕添加事件
public?void?actionPerformed(ActionEvent?e)?{//點擊按鈕,顯示信息到文本域
String?name?=?jtf1.getText();
String?number?=?jtf2.getText();
String?clazz?=?jtf3.getText();
jta.setText("You?name?is?"+name+"?number?is?"+number+"?class?is?"+clazz);
}
});
}
public?static?void?main(String[]?args)?{
new?FrameDemo().setVisible(true);//創(chuàng)建窗口,被設(shè)置為可見
}
}
五: 拓展
雖然圖形界面的實現(xiàn)方法是多樣的, ?我們一定要根據(jù)具體情況, 選擇一個比較優(yōu)化的 合理的, 符合業(yè)務(wù)邏輯的實現(xiàn)方法
急需一個java編程實現(xiàn)的簡單聊天窗口代碼
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientDemo01 {
public static void main(String[] args){
JFrame f=new JFrame("AA");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(15,30);
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
socket=new Socket("192.168.0.4",5000);
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread01 mt=new MyThread01(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener01(tf,ta,bos));
}
}
class ButtonActionListener01 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText();
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("AA:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失敗");
}
}
}
}
class MyThread01 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread01(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("BB:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
} import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ServerDemo01{
public static void main(String[] args){
JFrame f=new JFrame("BB");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTextArea ta=new JTextArea(12,30); //文本域,第一個參數(shù)為行數(shù),第二個參數(shù)為列數(shù)
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發(fā)送");
p1.add(sp);
p2.add(tf);
p2.add(b);
f.add(p1,"Center");
f.add(p2,"South");
f.setBounds(300,300,360,300);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ServerSocket server=null;
Socket socket=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
server=new ServerSocket(5000);
//ta.append("等待AA連接...\n");
socket=server.accept();
//ta.append("AA已連接\n");
bis=new BufferedInputStream(socket.getInputStream());
bos=new BufferedOutputStream(socket.getOutputStream());
MyThread1 mt=new MyThread1(bis,ta);
mt.start();
}catch(Exception e){
e.printStackTrace();
}
b.addActionListener(new ButtonActionListener1(tf,ta,bos));
}
}
class ButtonActionListener1 implements ActionListener{
JTextField tf;
JTextArea ta;
BufferedOutputStream bos;
public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){
this.tf=tf;
this.ta=ta;
this.bos=bos;
}
public void actionPerformed(ActionEvent e){
String message=tf.getText(); //獲取文本框中的內(nèi)容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發(fā)送失?。?);
}
}
}
}
class MyThread1 extends Thread{
BufferedInputStream bis;
JTextArea ta;
public MyThread1(BufferedInputStream bis,JTextArea ta){
this.bis=bis;
this.ta=ta;
}
public void run(){
try{
while(true){
byte[] b=new byte[100];
int length=bis.read(b);
String message=new String(b,0,length);
ta.append("AA:"+message+"\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
}
Java應(yīng)用程序怎樣點擊按鈕彈出文件查找路徑的窗口
1、js彈出文件選擇框:
給按鈕定義以下javascript函數(shù):
var inputObj=document.createElement('input')
inputObj.setAttribute('id','_ef');
inputObj.setAttribute('type','file');
inputObj.setAttribute("style",'visibility:hidden');
document.body.appendChild(inputObj);
inputObj.click();
inputObj.value ;
單擊已經(jīng)添加函數(shù)的按鈕會彈出選擇本地文件的對話框。
2、寫一個隱藏域, 當(dāng)用戶選擇文件之后把圖片的路徑賦給這個隱藏域, 然后在action中就可以獲取到文件的路徑了,代碼如下:
function showRealPath(filePath){
document.getElementsByName("textfield")[0].value = filePath;
}
input type="file" name="uploadfile" onfocus="showRealPath(this.value);"/
input type="hidden" name="uploadfileRealPath"
用java寫了一個界面,要求實現(xiàn)搜索功能,怎么做~?
實現(xiàn)方式有多種,建議方式一:
1. 在頁面制作好輸入框input,并且定義動作為打開一個幀iframe;
2. 在幀里,執(zhí)行動作為百度的鏈接。意思也就是百度執(zhí)行的結(jié)果在我自己的iframe里打開
本文題目:java搜索窗口代碼,搜索框java代碼
標(biāo)題來源:http://fisionsoft.com.cn/article/dsesdci.html