用eclipse设计一文件搜索程序,可按以下步骤实现:
?
创建一窗口框架;
? 输入要搜索的文件名(全名);
? 输入要搜索的文件夹名;
? 按下按钮后,在指定文件夹内(包括所有子文件夹)搜索指定文件。
? 搜索成功则在窗口上显示文件的全路径名,否则显示没有找到指定文件。
------解决方案--------------------
先列出该文件夹下的所有文件,然后逐个对比文件名,如果比对通过就返回路径。
如果是文件夹,则进行递归,直到所有文件搜索完毕。
------解决方案--------------------
package work;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class Find
{
private Frame f;
private TextField tf;
private Button b;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
public Find()
{
init();
}
public void init()
{
f = new Frame("my frame");
f.setBounds(300, 100, 600, 500);
f.setBackground(Color.lightGray);
f.setLayout(new FlowLayout());
tf = new TextField(60);
b = new Button("查询");
ta = new TextArea(25,70);
d = new Dialog(f,"提示信息",true);
d.setBounds(400,200,240,200);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("确定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(b);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) //匿名内部类
{
d.setVisible(false);
}
}
);
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
}
);
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
showDir();
}
});
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "您输入的信息有误:"+dirPath;
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new Find();
}
}
------解决方案--------------------
你试看看