📄 emailsearch.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/*
* 用于绘制图弄界面.
*/
class WindowFrame extends JFrame{
public static ReadLine rl;
final FileDialog open;//定义打开文件对话框
final FileDialog save;
BufferedReader in;//定义带缓冲的输入流
FileReader file_reader;//定义输入流
BufferedWriter out; //定义带缓冲的输出流
FileWriter tofile; //文件输出流
static JTextField path = new JTextField("c:\\",20);//声明路径条可以显示40个字符//用于显示文件路径的文件框
String spath;
static JTextArea content = new JTextArea(15,50);//声明邮件提取栏宽60字符,高20字符//用于存放提取后的邮箱地址
String dir,file;//用于临时存放文件路径及文件名
JScrollPane scrollPane ;//定义滚动列表
WindowFrame(){
//super(subject);//使用父类的构造方法用于显示标题.
setBounds(100,100,600,400);//定义程序界面大小
setLayout(new BorderLayout());//使用BorderLayout布局
final JButton start = new JButton("开始");//添加开始按钮
open = new FileDialog(this,"打开本地文件",FileDialog.LOAD);//声明打开文件对话框
save = new FileDialog(this,"保存邮箱地址",FileDialog.SAVE);
start.addActionListener(new ActionListener(){//添加开始按钮监视器
public void actionPerformed(ActionEvent e){
rl = new ReadLine();for(int i = 0;i<=10;i++){
MThread mt = new MThread();
Thread t = new Thread(mt);
t.start();
//System.out.println(WindowFrame.rl.getLine());
}
}
});
JButton bsave = new JButton("保存..");//定义保存按钮
bsave.addActionListener(new ActionListener(){//添加保存监视器
public void actionPerformed(ActionEvent e) {
save.setVisible(true);
if(save.getFile()!=null){
try{
File file=new
File(save.getDirectory(),save.getFile());
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(content.getText(),0,(content.getText()).length());
out.flush();
out.close();
tofile.close();
}
catch(IOException e2){}
}
}
});
JButton bopen = new JButton("打开..");//定义打开按钮
bopen.addActionListener(new ActionListener(){//添加打开监视器
public void actionPerformed(ActionEvent e){
open.setVisible(true);//显示打开对话框
if(open.getFile()!=null)//如果选择文件不为空
{
dir = open.getDirectory();//dir等于打开文件目录,
file = open.getFile();//file等于打开文件名
path.setText(open.getDirectory()+open.getFile());//设置路径为dir+file
spath=path.getText();
}
}
});
JPanel up = new JPanel();//声明上面板
JPanel down = new JPanel();//声明下面板
up.add(new Label("文件路径"));up.add(path);
up.add(bopen);up.add(start);up.add(bsave);//将文件路径和按钮添加到上面板中
scrollPane = new JScrollPane(content);//为提取后的邮箱增加滚动列表
down.add(scrollPane);//将邮箱提取窗口放到下面板中
add(up,BorderLayout.NORTH);//将上面析添加到窗口的北面
add(down,BorderLayout.CENTER);//将下面板添加到窗口中间
}
}
/*
* 此类用于分析传递过来的字串中是否包含邮箱地址,然后进行提取.
*/
class AnalyseChar{
int sta=0,end=0;//定义提取时的起始字串与结束字串
char[]ch; //定义分析字串数组
AnalyseChar(String s){
ch=s.toCharArray();//把传进来的S分解成单个字符进行分析
for(int i=0;i<s.length();i++){
breakit: //跳出循环
if(ch[i]=='@'){//如果此行包括"@"符号
if((Character.isDigit(ch[i+1])||Character.isLetter(ch[i+1])||(ch[i+1]=='_'))){//如果"@"符号后边是数字||字母||下划线
for(int k=i+1;k<s.length();k++){
if(ch[k]=='.'){//如果@之后的数字||字母||下划线||后边有"."
if(Character.isDigit(ch[k+1])||Character.isLetter(ch[k+1]))//如果点后边的字母是数字||字母
for(int j=k+1;j<=s.length();j++){
if((j==s.length())||(!(Character.isDigit(ch[j])||Character.isLetter(ch[j])||(ch[j]=='_')||(ch[j]=='.')))){//如果查到最后头了,或者查到后边不是数字||字母||下划线||"."的字符,也就是邮件结束的位置
end=j;//结束位置就等于邮箱的后一个字符
for(int l=i-1;l>=0;l--){//从"@"向前查找
if((!(Character.isDigit(ch[l])||Character.isLetter(ch[l])||(ch[l]=='_')||(ch[l]=='.')))){//如果查到最前头了,或者"@"前边有不是数字||字母||下划线||和"."的字符
if((l+1)!=i){//如果最前一个字符就是i也就是@那么这条邮件就是假邮件.
sta=l+1;//起始位置就等于当前字符的后一位
//WindowFrame.content.append(sta+"\n");//将字串添加到文本列表内
String email = s.substring(sta,end);//邮箱地址等于起始符与结束符中间的字串
WindowFrame.content.append(email+"\r\n");//将字串添加到文本列表内
break breakit;//跳出循环
}
}
else if(l==0){
sta=l;//否则如果查到头了也没有找到一个不是邮件字符的字符那么这个字符就是这个邮件的邮件头字符.
//WindowFrame.content.append(sta+"\n");//将字串添加到文本列表内
String email = s.substring(sta,end);//邮箱地址等于起始符与结束符中间的字串
WindowFrame.content.append(email+"\r\n");//将字串添加到文本列表内
break breakit;//跳出循环
}
}
}
}
}
}
}
}
}
}
}
class ReadLine{
BufferedReader in;//定义带缓冲的输入流
FileReader file_reader;//定义输入流
BufferedWriter out; //定义带缓冲的输出流
FileWriter tofile; //文件输出流
String s;
File filein;
int length;
ReadLine(){
if(!(WindowFrame.path.getText()==null))//如果打开文件的路径不为空
{
try{ filein= new File(WindowFrame.path.getText());//读取文件等于路径条显示的文件
file_reader=new FileReader(filein);//读取文件
in=new BufferedReader(file_reader);//缓冲读取文件
}
catch(IOException e2){WindowFrame.content.setText("路径不正确");}
}
else{
WindowFrame.content.setText("没有选择文件");
}
}
public String getLine(){
try{
s=in.readLine();
}
catch(IOException e2){}
catch(NullPointerException e3){}
return s;
}
}
class MThread implements Runnable{
String s ;
public void run() {
while((s=WindowFrame.rl.getLine())!=null){//如果读入一行字串不为空,s等于读取那一行的字串.
new AnalyseChar(s);//将s传递给字串份析类
if(s==null){
return;
}
}
}
}
/*
* 主类
*/
public class EmailSearch {
public static void main(String []args){
WindowFrame frame = new WindowFrame();//启用程序界面,并将字串传递到窗口类中.显示标题.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -