Java 对txt文件操作


上图中.

   OK文件夹:存放分隔后的txt文件,因为: Excel 2003一个工作表最多可有65536行,而B.txt中存在65W数据,所以得将数据进行分解保存到此目录。

   A.txt : 已经存在的数据

   B.txt : 所有的数据

   E.txt : B.txt-A.txt - 重复数据。


java代码


package com.gl.test;
 
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.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
 
/**
 * 从B.txt文件中过滤掉A.txt存在的文件 实现功能: 1、先将A中已发送的邮箱去除 2、去除后再取只要qq.com后辍的邮箱 3、去重
 */
public class No3 {
 
    private static Set sendedLst = new HashSet();// 已发送的邮箱
    private static Map allLst = new HashMap(); // 未发送的邮箱
    private static Set noSaveLst = new HashSet();// 已发送的邮箱
    private static long index=1;
    private static int current=1;
    final static String send_xls_path="D:\\\\REMOVE\\\\OK\\\\";
     
    public static void main(String[] args) throws IOException {
        saveInE();//将数据保存在E.txt文件
    }
    private static void saveInE(){
        final String sended_path = "D:\\REMOVE\\A.txt";// 已发送的地址
        final String all_email_path = "D:\\REMOVE\\B.txt";// 所有的邮箱地址
        String save_path = "D:\\REMOVE\\E.txt";
        try {
            long startTime=System.currentTimeMillis(); 
            readSendText(new File(sended_path));
            readAllText(new File(all_email_path));
            BufferedWriter br = new BufferedWriter(new FileWriter(save_path));
            for(String str : allLst.keySet()) {   
                br.write(str + "\r\n");
            }  
            br.close();
             
             
             
            BufferedWriter temp_t = new BufferedWriter(new FileWriter(send_xls_path+current+".txt"));
             for(Iterator iterator = noSaveLst.iterator(); iterator.hasNext();) {
                    String str = iterator.next();
                    temp_t.write(str + "\r\n");
            }
            temp_t.close();
             
            long endTime=System.currentTimeMillis(); //获取结束时间  
            System.out.println("程序运行时间: "+(endTime-startTime)/1000+"s");   
             
            System.out.println("---------send count = " + sendedLst.size()+ "-------------");
            System.out.println("-----------no send count = " + allLst.size()+ "---------------");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
 
    private static void readAllText(File file) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            removeEmailText(line);
        }
        br.close();
    }
 
    private static  void removeEmailText(String str) throws Exception {
        if (!sendedLst.contains(str)) {// 如果已发送的邮箱不包含str这个邮箱
            String endWith = str.substring(str.indexOf("@") + 1).toLowerCase();
            if ("qq.com".equals(endWith) || "vip.qq.com".equals(endWith)) {// QQ邮箱
                String strQQ = str.toLowerCase();
                if (!allLst.containsKey(strQQ)) {
                     
                    //将数据进行分解保存
                    if(index`000==0){
                        BufferedWriter temp_t = new BufferedWriter(new FileWriter(send_xls_path+current+".txt"));
                         for(Iterator iterator = noSaveLst.iterator(); iterator.hasNext();) {
                                String qq = iterator.next();
                                temp_t.write(qq + "\r\n");
                        }
                        temp_t.close();
                        noSaveLst.clear();
                        current++;
                    }
                    index++;
                    noSaveLst.add(strQQ);
                     
                     
                    allLst.put(strQQ,strQQ);
                }
            }
        }
    }
 
    private static void putSendStr(String str) {
            sendedLst.add(str);
    }
 
    private static void readSendText(File file) throws Exception {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line = null;
        while ((line = br.readLine()) != null) {
            putSendStr(line);
        }
        br.close();
    }
}


输出结果


程序运行时间: 1s
---------send count = 85409-------------
-----------no send count = 589066---------------