正则表达式邮件地址提取_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 正则表达式邮件地址提取

正则表达式邮件地址提取

 2012/6/11 0:11:50  sjp524617477  程序员俱乐部  我要评论(0)
  • 摘要:publicclassEmailExtractextendsJFrame{privatestaticfinallongserialVersionUID=1L;publicEmailExtract(){this.setSize(400,150);this.setLocation(350,300);this.setTitle("邮件地址抽取");}publicstaticvoidmain(String[]args){EmailExtractframe=newEmailExtract();frame
  • 标签:邮件 正则表达式 表达式 正则
public class EmailExtract extends JFrame{

	private static final long serialVersionUID = 1L;

	public EmailExtract(){
		this.setSize(400, 150);
		this.setLocation(350, 300);
		this.setTitle("邮件地址抽取");
	}
	
	public static void main(String[] args) {
		EmailExtract frame = new EmailExtract();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new EmailExtractPanel());
		frame.setVisible(true);
	}
}

class EmailExtractPanel extends JPanel{
	
	private static final long serialVersionUID = 1L;
	
	private JTextField inField = new JTextField(20);
	private JTextField outField = new JTextField(20);
	public EmailExtractPanel(){
		this.setLayout(new GridLayout(3, 1));
		
		JPanel panel1 = new JPanel();
		panel1.add(new JLabel("输入"));
		panel1.add(inField);
		JButton button1 = new JButton("选择");
		button1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				int i = chooser.showOpenDialog(EmailExtractPanel.this);
			    if (i == JFileChooser.APPROVE_OPTION) {
			    	inField.setText(chooser.getSelectedFile().getAbsolutePath());
			    }
			}
		});
		panel1.add(button1);
		this.add(panel1);
		
		JPanel panel2 = new JPanel();
		panel2.add(new JLabel("输出"));
		panel2.add(outField);
		JButton button2 = new JButton("选择");
		button2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int i = chooser.showOpenDialog(EmailExtractPanel.this);
			    if (i == JFileChooser.APPROVE_OPTION) {
			    	outField.setText(chooser.getSelectedFile().getAbsolutePath());
			    }
			}
		});
		panel2.add(button2);
		this.add(panel2);
		
		JPanel panel3 = new JPanel();
		JButton okButton = new JButton("确定");
		okButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(!"".equals(inField.getText()) && !"".equals(outField.getText())){
					File inFile = new File(inField.getText());
					File outFolder = new File(outField.getText() + "\\邮件地址输出");
					if(!outFolder.exists()){
						outFolder.mkdir();
					}
					try {
						emialExport(inFile, outFolder);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					System.exit(0);
				}
			}
		});
		panel3.add(okButton);
		this.add(panel3);
	}
	
	private void emialExport(File inFile, File outFolder) throws IOException{
		if(inFile.isDirectory()){
			File loutFolder = new File(outFolder, inFile.getName());
			if(!loutFolder.exists()){
				loutFolder.mkdir();
			}
			File[] fileList = inFile.listFiles();
			for(File file : fileList){
				emialExport(file, loutFolder);
			}
		}else{
			File toFile = new File(outFolder, inFile.getName());
			FileWriter fw = new FileWriter(toFile);
			fw.append(getEmailList(getStringFromFile(inFile)));
			fw.flush();
			fw.close();
		}
	}
	
	public String getStringFromFile(File f) throws FileNotFoundException{
		StringBuilder sb = new StringBuilder("");
		Scanner in = new Scanner(f);
		while(in.hasNextLine()){
			sb.append(" " + in.nextLine());
		}
		in.close();
		return sb.toString();
	}
	
	private String getEmailList(String emailText){
		String email = "";
		String emailList = "";
		Pattern p = Pattern.compile("[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+");   
        Matcher m = p.matcher(emailText);   
        while (m.find())
        {
        	email = m.group();
        	emailList += email + ";";
        }
        return emailList;
	}
}
  • 邮件地址抽取.zip (191.7 KB)
  • 下载次数: 0
发表评论
用户名: 匿名