loademails.java
来自「基于java的电子邮件群发系统,基于java的电子邮件群发系统」· Java 代码 · 共 90 行
JAVA
90 行
package com.cwq.batchmail.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import com.cwq.batchmail.mail.AbstractMailThreads;
public class LoadEmails {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//File f = new File("F:\\eclipse-SDK-3.2-win32\\workspace\\batchmail\\src\\res\\emails.txt");
//try { clean(f); } catch(Exception ex) { ex.printStackTrace(); }
}
//public static final String mailRegex = "([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-|\\.)+\\.)+[a-z]{2,6}";
private int totalEmailsCount = 0;
Set<String> load(File f) throws IOException {
Set<String> emails = new HashSet<String>();
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if(line.length() > 0 && line.matches(AbstractMailThreads.mailRegex)) {
emails.add(line);
totalEmailsCount ++;
}
}
try { reader.close(); } catch(Exception ex) {}
return emails;
}
public Set<String> load(File f, final int limit) throws IOException {
Set<String> emails = new HashSet<String>();
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = null;
while ((line = reader.readLine()) != null) {
line = line.trim();
if(line.length() > 0 && line.matches(AbstractMailThreads.mailRegex)) {
totalEmailsCount ++;
if(totalEmailsCount <= limit)
emails.add(line);
}
}
try { reader.close(); } catch(Exception ex) {}
return emails;
}
public Set<String> clean(File f) throws IOException {
Set<String> emails = load(f);
save(f, emails);
return emails;
}
static void save(File f, Set<String> emails) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(f));
final int BUFFER_SIZE = 1024;
int count = 0;
for(Iterator<String> it = emails.iterator(); it.hasNext(); ) {
writer.write(it.next().concat("\r\n"));
if(count ++ > BUFFER_SIZE)
writer.flush();
}
writer.flush();
try { writer.close(); } catch(Exception ex) {}
}
public int getTotalEmailsCount() {
return totalEmailsCount;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?