锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
public class SimpleSearcherFrame {
JFrame jf=new JFrame("SimpleSearcher");
JTextArea jta;
JLabel l1,l2;
JPanel p;
JTextField tf1,tf2;
JScrollPane jp;
JButton b1,b2,b3;
FileDialog fd;
String keyword,parent,child,path=new String();
public void go() throws IOException{
l1=new JLabel("璇瘋緭鍏ユ枃浠舵墍鍦ㄤ綅緗?);
tf1=new JTextField(20);
tf1.setEditable(true);
l2=new JLabel("璇瘋緭鍏ユ煡璇㈢殑鍏抽敭瀛?);
tf2=new JTextField(20);
tf2.setEditable(true);
keyword=tf2.getText();
{fd=new FileDialog(jf,"鎵撳紑鏂囦歡浣嶇疆");
jta=new JTextArea(300,100);
jp=new JScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
b1=new JButton("鎵撳紑鏂囦歡浣嶇疆");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
fd.setVisible(true);
parent=fd.getDirectory();
child=fd.getFile();
path=parent+child;
tf1.setText(path);
}
}
);
b2=new JButton("鏌ヨ");
b3=new JButton("娓呯┖");
b3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
jta.setText(null);
}
});
p=new JPanel();
p.add(l1);
p.add(tf1);
p.add(b1);
p.add(tf2);
p.add(b2);
Container cp=jf.getContentPane();
cp.add(p,BorderLayout.NORTH);
cp.add(jta,BorderLayout.CENTER);
cp.add(b3,BorderLayout.SOUTH);
jf.pack();
jf.setVisible(true);
SimpleSearcherCode ssc=new SimpleSearcherCode();
String s=ssc.search(keyword,path);
jta.append(s);
}
}
}
package SimpleSearcher;
import java.io.*;
class SimpleSearcherCode
{
public String search(String keyword,String path) throws IOException
{
FileReader fr = new FileReader(path);
BufferedReader reader=new BufferedReader(fr);
int line=0;int mark=0;String s1[]=new String[300];int i=0;
while(reader.readLine()!=null)
{
line++;
int start=0;
while(reader.readLine().indexOf(keyword, start)!=-1){
start=reader.readLine().indexOf(keyword,start);
mark++;
s1[i]= keyword+"鍦ㄧ"+line+"絎?+start+"浣嶇疆";
i++;
}
}
String s2="鏂囦歡涓竴鍏辨悳鍒?+mark+"涓叧閿瓧";
if(mark==0)
{
return "鏂囦歡涓病鏈夌浉鍏沖唴瀹?;
}
else {for(int j=0;j<i;j++)
s2+=s1[i];
return s2;
}
}
}
package SimpleSearcher;
import java.io.IOException;
public class Searching {
public static void main(String args[]) throws IOException
{
SimpleSearcherFrame ssf =new SimpleSearcherFrame();
ssf.go();
}
}
鐣岄潰娌′粈涔堥棶棰橈紝涓昏鏄湪鏂囦歡嫻佺殑杞崲涓婁笉鐭ラ亾鍝噷涓嶅尮閰嶏紝鑰佹槸璇存病瀹炰緥鍖栥?/p>