• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
习惯不习惯的习惯
博客园    首页    新随笔    联系   管理    订阅  订阅

2个list比较将过滤的差异内容写入文件

 

因为老大让我统计下两个保单差异,由于一条条的去看比较麻烦,费时间,于是就想想利用程序去做比较,最后就有了这本文。

不废话直接上代码,新手,有不足可优化之处,请大家指出,我会修改的。

 

 

  1 package policy;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileWriter;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.io.Writer;
 10 import java.util.ArrayList;
 11 import java.util.List;
 12 
 13 /**
 14 * FileName: Test3
 15 * Author: wl
 16 * Date: 2019年7月16日11:42:22
 17 * Description: 
 18 */
 19 public class Test3 {
 20         
 21     
 22     private static String filePath1 = "C:\\Users\\szwb084\\Desktop\\滴滴单量统计\\滴滴成功(4080,1014).txt";
 23     private static String filePath2 = "C:\\Users\\szwb084\\Desktop\\滴滴单量统计\\太平policy_no.txt";
 24     //读取2个文件的保单内容
 25     private static String result = readFile(filePath1);
 26     //读取2个文件的保单内容
 27     private static String result2 = readFile(filePath2);
 28     private static List<String> policyListTaiping = new ArrayList<String>();
 29     private static List<String> policyListDD = new ArrayList<String>();
 30 
 31 
 32     public static void main(String[] args) {
 33         
 34         loadList();
 35         List listDiff = new ArrayList();
 36         String results = "";
 37         String line = System.getProperty("line.separator");//分行符号
 38         for (int i = 0; i < policyListDD.size(); i++) {
 39             String a = (String) policyListDD.get(i);
 40             int flag = 0;
 41             for (int j = 0; j < policyListTaiping.size(); j++) {
 42                 String b = (String) policyListTaiping.get(j);
 43                 if (a.equals(b)) {
 44                     flag = flag + 1;
 45                 }
 46             }
 47             // 原理 : 拿到一个list里面的保单到list2里面循环 -如果不存在就添加到新的list
 48             if (flag == 0) {
 49                 listDiff.add(a); //存入差异内容
 50             }
 51         }
 52 
 53         for (int i = 0; i < listDiff.size(); i++) {
 54             results = results + listDiff.get(i) + "," + line; //拼接保单信息
 55         }
 56         
 57         if (!"".equals(results) && null != results) {
 58             writeContent(results,"D:\\2.txt");
 59         }
 60     }
 61     
 62     
 63     /**
 64      *   将内容写入磁盘
 65      * @param results
 66      * @param filePath
 67      */
 68     public static void writeContent(String results,String filePath) {
 69         
 70         File file = new File(filePath);//
 71         Writer out;
 72         try {
 73             out = new FileWriter(file);
 74             out.write(results);
 75             out.close();
 76         } catch (IOException e) {
 77             e.printStackTrace();
 78         }
 79     }
 80     
 81     
 82     /**
 83      *   加载list比较数据
 84      */
 85     public static void loadList() {
 86         
 87         if (null != result && !"".equals(result)) {
 88             String[] str = result.split(",");
 89             for (String policy : str) {
 90                 policyListTaiping.add(policy);
 91             }
 92         }
 93 
 94         if (null != result2 && !"".equals(result2)) {
 95             String[] str2 = result2.split(",");
 96             for (String policy : str2) {
 97                 policyListDD.add(policy);
 98             }
 99         }
100     }
101     
102     
103     /**
104      *       读取文件内容
105      * @param filePath
106      * @return
107      */
108     public static String readFile(String filePath) {
109         StringBuffer message = new StringBuffer();
110         try {
111             String encoding = "UTF-8";
112             File file = new File(filePath);
113             if (file.isFile() && file.exists()) { // 判断文件是否存在
114                 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
115                 BufferedReader bufferedReader = new BufferedReader(read);
116                 String lineTxt = null;
117                 while ((lineTxt = bufferedReader.readLine()) != null) {
118                     message.append(lineTxt);
119                 }
120                 read.close();
121             } else {
122                 System.out.println("找不到指定的文件");
123             }
124         } catch (Exception e) {
125             System.out.println("读取文件内容出错");
126             e.printStackTrace();
127         }
128         return message.toString();
129 
130     }
131     
132     
133     
134 }

 

 

最后写入的文件是这样子的

 

 

 

posted on 2019-07-16 11:26  习惯不习惯的习惯  阅读(285)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3