需求:
在表A:S2s中有3个字段,cust_id,form_date和isTransmit,在表B:Flag中,有3个字段:cust_id、startDate和endDate,请找出S2s中的某些记录,约束条件如下:
1、该笔记录的cust_id存在于表Flag中,即S2s.cust_id=Flag.cust_id;
2、该笔记录的isTransmit值为N;
3、该笔记录的form_date在表Flag的startDate和endDate之间,即form_date>=startDate且form_date<=endDate;
4、form_date格式为YYYYMMDD,startDate和endDate格式为YYYY-MM-DD;
由于Production环境的资料需要保密,只能提供表A的3个自动和表B的3字段,现在客户已经导出到txt文档,每笔记录包含3个字段,以空格分开。
下面我们分步骤开实现如何测试客户提供的数据:
1、创建2个类,将文本文件中的数据实例化对象;
public class S2s { private String cust_id; private String form_date; private String isTransmit; /** * @return the cust_id */ public String getCust_id() { return cust_id; } /** * @param cust_id the cust_id to set */ public void setCust_id(String cust_id) { this.cust_id = cust_id; } /** * @return the form_date */ public String getForm_date() { return form_date; } /** * @param form_date the form_date to set */ public void setForm_date(String form_date) { this.form_date = form_date; } /** * @return the isTransmit */ public String getIsTransmit() { return isTransmit; } /** * @param isTransmit the isTransmit to set */ public void setIsTransmit(String isTransmit) { this.isTransmit = isTransmit; } }
public class Flag { private String customer_id; private String startDate; private String endDate; /** * @return the customer_id */ public String getCustomer_id() { return customer_id; } /** * @param customer_id the customer_id to set */ public void setCustomer_id(String customer_id) { this.customer_id = customer_id; } /** * @return the startDate */ public String getStartDate() { return startDate; } /** * @param startDate the startDate to set */ public void setStartDate(String startDate) { this.startDate = startDate; } /** * @return the endDate */ public String getEndDate() { return endDate; } /** * @param endDate the endDate to set */ public void setEndDate(String endDate) { this.endDate = endDate; } }
2、分行读取文本文件,并将每条记录序列化为对象,存储到List中:
我们先来看看txt文档中的片段:
S223821747 2008-09-05 2008-09-15 A227577478 2008-09-05 2008-09-15 L123462919 2008-09-05 2008-09-15 S124039554 2008-09-05 2008-09-15 F224800911 2008-09-05 2008-09-15 A225667273 2008-09-05 2008-09-15 F223968423 2008-09-05 2008-09-15 Z100098584 2008-09-05 2008-09-30 H122644620 2008-09-05 2008-09-30 U221392263 2008-09-05 2008-09-30 A227669499 2008-09-05 2008-09-30 N224708641 2008-09-05 2008-09-30 B290023255 2008-09-05 2008-09-30 C221114157 2008-09-05 2008-09-30 T220902657 2008-09-05 2008-09-30
从中我们可以看到,3个字段以空格分隔,其中第一个字段和第二个字段中间有多个空格;
FileReader reader = new FileReader(flagFileName); BufferedReader br = new BufferedReader(reader); String s1 = null; while ((s1 = br.readLine()) != null) { //TODO }
3、将得到的字符串分割为字符数组(String[]):
s2 = s1.split(" ");
4、通过打印字符数组可知s2[0]、s2[6]、s2[7]为3个字段的位置,将3个字段的值放入对象并存储到List中:
f = new Flag(); f.setCustomer_id(s2[0]); f.setStartDate(s2[7]); f.setEndDate(s2[8]); flagList.add(f);
该方法的完整代码如下:
/** * 从Flag 的txt文件中按行读取,将得到的3个字符串分别放入Flag对象中, * 然后将对于存储在返回List中 * @return */ public static List readFlag() { FileReader reader = null; List flagList = new ArrayList(); try { reader = new FileReader(flagFileName); BufferedReader br = new BufferedReader(reader); String s1 = null; String[] s2 = null; Flag f = null; int i = 0; int j = 0; while ((s1 = br.readLine()) != null) { s2 = s1.split(" "); // if (s2.length != 9) { f = new Flag(); f.setCustomer_id(s2[0]); f.setStartDate(s2[6]); f.setEndDate(s2[7]); i++; j++; flagList.add(f); continue; } f = new Flag(); f.setCustomer_id(s2[0]); f.setStartDate(s2[7]); f.setEndDate(s2[8]); i++; flagList.add(f); } br.close(); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return flagList; }
5、判断时间区间,由于时间格式不同,先要去掉startDate和endDate的“-”,用String的replaceAll()即可,然后就可以直接比较了:
/** * 先将Flag对象中的startDate和endDate的格式由YYYY-MM-DD转换为YYYYMMDD * 然后看S2s对象中的formDate是否: * formDate.compareTo(startDate) >= 0) * && (formDate.compareTo(endDate) <= 0 * 比对结果为返回值 * @param s * @param f * @return */ public static boolean inDateField(S2s s, Flag f) { String formDate = s.getForm_date(); if (f == null) { return false; } String startDate = f.getStartDate(); String endDate = f.getEndDate(); if (formDate == null || "".equals(formDate) || startDate == null || "".equals(startDate) || endDate == null || "".equals(endDate)) { return false; } startDate = startDate.replaceAll("-", ""); endDate = endDate.replaceAll("-", ""); return (formDate.compareTo(startDate) >= 0) && (formDate.compareTo(endDate) <= 0); }
6、最后我们来看S2s中的cust_id是否存在于表Flag中,我们先读取Flag中的所有记录,然后将所有cust_id存于一个HashSet中,同时以cust_id为Key,以Flag对象为Value,存储到一个HashMap中;然后使用HashSet的contains()看cust_id是否存在于此HashSet中:
custIdSet.contains(s.getCust_id()
完整代码如下:
public static void compareId() { List s2sList = getListS2sN(readS2s()); List flagList = readFlag(); Flag f = null; // 存放“新開戶ID清單”中的ID Set custIdSet = new HashSet(); Map beansMap = new HashMap(); int refSize2 = 0; if (flagList != null && flagList.size() > 0) { refSize2 = flagList.size(); } for (int i = 0; i < refSize2; i++) { f = (Flag) flagList.get(i); custIdSet.add(f.getCustomer_id()); beansMap.put(f.getCustomer_id(), f); } System.out.println("custIdSet.size(): " + custIdSet.size()); System.out.println("beansMap.size(): " + beansMap.size()); int refSize = 0; if (s2sList != null && s2sList.size() > 0) { refSize = s2sList.size(); } System.out.println("s2sList.size(): " + refSize); S2s s = null; for (int i = 0; i < refSize; i++) { s = (S2s) s2sList.get(i); f = (Flag) beansMap.get(s.getCust_id()); System.out.println(custIdSet.contains(s.getCust_id()) + " : " + inDateField(s, f) + " : " + s.getCust_id() + ":"); if (custIdSet.contains(s.getCust_id()) && inDateField(s, f)) { System.out.println("####" + i + ":" + custIdSet.contains(s.getCust_id()) + " : " + inDateField(s, f) + " : " + s.getCust_id() + ":"); } } }
7、其他:
可以使用POI生成Excel档,代码如下:
/** * 将S2s的List生成Excel档 */ public static void createExcelS2s() { String xlsFile = "D:/0Excel/S2s.xls"; List s2sList = getListS2sN(readS2s()); try { // 产生工作簿对象 HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作表对象,设置工作表的名称为Sheet1 HSSFSheet sheet = workbook.createSheet("Sheet1"); sheet.setColumnWidth((short) 0, (short) 4000); sheet.setColumnWidth((short) 1, (short) 2000); sheet.setColumnWidth((short) 2, (short) 3000); // 产生一行 HSSFRow row = sheet.createRow((short) 0); // 产生第1个单元格 客戶ID或卡號 HSSFCell cell_01 = row.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); cell_01.setCellValue(new HSSFRichTextString("cust_id")); // 产生第2个单元格 BLK HSSFCell cell_02 = row.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); cell_02.setCellValue(new HSSFRichTextString("form_date")); // 产生第3个单元格 處理時間 HSSFCell cell_03 = row.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); cell_03.setCellValue(new HSSFRichTextString("isTransmit")); HSSFCell newCell_01 = null; HSSFCell newCell_02 = null; HSSFCell newCell_03 = null; int s2sSize = 0; if (s2sList != null) { s2sSize = s2sList.size(); } S2s s = null; for (int i = 1; i < s2sSize; i++) { s = (S2s) s2sList.get(i); // 产生一行 HSSFRow rowNew = sheet.createRow((short) i); newCell_01 = rowNew.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); newCell_01.setCellValue(new HSSFRichTextString(s.getCust_id())); // 产生第2个单元格 BLK newCell_02 = rowNew.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); newCell_02 .setCellValue(new HSSFRichTextString(s.getForm_date())); // 产生第3个单元格 處理時間 newCell_03 = rowNew.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); newCell_03.setCellValue(new HSSFRichTextString(s .getIsTransmit())); } FileOutputStream fOut = new FileOutputStream(xlsFile); workbook.write(fOut); fOut.flush(); fOut.close(); System.out.println("文件生成..."); // 以下语句读取生成的Excel文件内容 FileInputStream fIn = new FileInputStream(xlsFile); HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn); HSSFSheet readSheet = readWorkBook.getSheet("Sheet1"); HSSFRow readRow = readSheet.getRow(0); HSSFCell readCell = readRow.getCell((short) 0); System.out.println("第一個單元是:" + readCell.getRichStringCellValue()); } catch (Exception e) { e.printStackTrace(); System.out.println(e); } }
整个测试类的完整代码如下:
package com.coderdream.s2s; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class Test { /** S2S_txt文件位置 */ private static String s2sFileName = "S2S_1.txt"; /** Flag_txt文件位置 */ private static String flagFileName = "Flag_1.txt"; /** * @param args */ public static void main(String[] args) { //readS2s(); //printListS2s(readS2s()); //printListS2s(getListS2sN(readS2s())); //printListFlag(readFlag()); //compareId(); //testSet(); //createExcel(); //createExcelFlag(); compareId(); //testReplaceAll(); } /** * 从S2s 的txt文件中按行读取,将得到的3个字符串分别放入S2s对象中, * 然后将对于存储在返回List中 * * @return */ public static List readS2s() { FileReader reader = null; List s2sList = new ArrayList(); try { reader = new FileReader(s2sFileName); BufferedReader br = new BufferedReader(reader); String s1 = null; String[] s2 = null; S2s s = null; while ((s1 = br.readLine()) != null) { // 根据导出的文本可知,字符串以空格分开 s2 = s1.split(" "); s = new S2s(); s.setCust_id(s2[0]); s.setForm_date(s2[7]); s.setIsTransmit(s2[9]); s2sList.add(s); } br.close(); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return s2sList; } /** * 从Flag 的txt文件中按行读取,将得到的3个字符串分别放入Flag对象中, * 然后将对于存储在返回List中 * @return */ public static List readFlag() { FileReader reader = null; List flagList = new ArrayList(); try { reader = new FileReader(flagFileName); BufferedReader br = new BufferedReader(reader); String s1 = null; String[] s2 = null; Flag f = null; int i = 0; int j = 0; while ((s1 = br.readLine()) != null) { s2 = s1.split(" "); // if (s2.length != 9) { f = new Flag(); f.setCustomer_id(s2[0]); f.setStartDate(s2[6]); f.setEndDate(s2[7]); i++; j++; flagList.add(f); continue; } f = new Flag(); f.setCustomer_id(s2[0]); f.setStartDate(s2[7]); f.setEndDate(s2[8]); i++; flagList.add(f); } br.close(); reader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return flagList; } /** * @param s2 */ public static void printArray(String[] s2) { for (int i = 0; i < s2.length; i++) { if (s2[i] != null && !"".equals(s2[i].trim())) { System.out.print(s2[i] + ":"); } } System.out.println(); } /** * @param s2sList */ public static void printListS2s(List s2sList) { S2s s = null; int size = 0; if (s2sList != null) { size = s2sList.size(); } int index = 0; for (int i = 0; i < size; i++) { s = (S2s) s2sList.get(i); index = i + 1; System.out.println(index + "\t:" + s.getCust_id() + ":" + s.getForm_date() + ":" + s.getIsTransmit()); } } /** * @param s2s */ public static void printS2s(S2s s2s) { if (s2s != null) { System.out.println(s2s.getCust_id() + ":" + s2s.getForm_date() + ":" + s2s.getIsTransmit()); } } /** * @param s2sList * @return */ public static List getListS2sN(List s2sList) { List list = new ArrayList(); S2s s = null; int size = 0; if (s2sList != null) { size = s2sList.size(); } for (int i = 0; i < size; i++) { s = (S2s) s2sList.get(i); if ("N".equals(s.getIsTransmit())) { list.add(s); } } return list; } /** * @param flagList */ public static void printListFlag(List flagList) { Flag f = null; int size = 0; if (flagList != null) { size = flagList.size(); } int index = 0; for (int i = 0; i < size; i++) { f = (Flag) flagList.get(i); index = i + 1; System.out.println(index + "\t:" + f.getCustomer_id() + ":" + f.getStartDate() + ":" + f.getEndDate()); } } /** * @param flag */ public static void printFlag(Flag flag) { if (flag != null) { System.out.println(flag.getCustomer_id() + ":" + flag.getStartDate() + ":" + flag.getEndDate()); } } /** * 先将Flag对象中的startDate和endDate的格式由YYYY-MM-DD转换为YYYYMMDD * 然后看S2s对象中的formDate是否: * formDate.compareTo(startDate) >= 0) * && (formDate.compareTo(endDate) <= 0 * 比对结果为返回值 * @param s * @param f * @return */ public static boolean inDateField(S2s s, Flag f) { String formDate = s.getForm_date(); if (f == null) { return false; } String startDate = f.getStartDate(); String endDate = f.getEndDate(); if (formDate == null || "".equals(formDate) || startDate == null || "".equals(startDate) || endDate == null || "".equals(endDate)) { return false; } startDate = startDate.replaceAll("-", ""); endDate = endDate.replaceAll("-", ""); return (formDate.compareTo(startDate) >= 0) && (formDate.compareTo(endDate) <= 0); } /** * */ public static void compareId() { List s2sList = getListS2sN(readS2s()); List flagList = readFlag(); Flag f = null; // 存放“新開戶ID清單”中的ID Set custIdSet = new HashSet(); Map beansMap = new HashMap(); int refSize2 = 0; if (flagList != null && flagList.size() > 0) { refSize2 = flagList.size(); } for (int i = 0; i < refSize2; i++) { f = (Flag) flagList.get(i); custIdSet.add(f.getCustomer_id()); beansMap.put(f.getCustomer_id(), f); } System.out.println("custIdSet.size(): " + custIdSet.size()); System.out.println("beansMap.size(): " + beansMap.size()); int refSize = 0; if (s2sList != null && s2sList.size() > 0) { refSize = s2sList.size(); } System.out.println("s2sList.size(): " + refSize); S2s s = null; for (int i = 0; i < refSize; i++) { s = (S2s) s2sList.get(i); f = (Flag) beansMap.get(s.getCust_id()); System.out.println(custIdSet.contains(s.getCust_id()) + " : " + inDateField(s, f) + " : " + s.getCust_id() + ":"); if (custIdSet.contains(s.getCust_id()) && inDateField(s, f)) { System.out.println("####" + i + ":" + custIdSet.contains(s.getCust_id()) + " : " + inDateField(s, f) + " : " + s.getCust_id() + ":"); } } } /** * 将S2s的List生成Excel档 */ public static void createExcelS2s() { String xlsFile = "D:/0Excel/S2s.xls"; List s2sList = getListS2sN(readS2s()); try { // 产生工作簿对象 HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作表对象,设置工作表的名称为Sheet1 HSSFSheet sheet = workbook.createSheet("Sheet1"); sheet.setColumnWidth((short) 0, (short) 4000); sheet.setColumnWidth((short) 1, (short) 2000); sheet.setColumnWidth((short) 2, (short) 3000); // 产生一行 HSSFRow row = sheet.createRow((short) 0); // 产生第1个单元格 客戶ID或卡號 HSSFCell cell_01 = row.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); cell_01.setCellValue(new HSSFRichTextString("cust_id")); // 产生第2个单元格 BLK HSSFCell cell_02 = row.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); cell_02.setCellValue(new HSSFRichTextString("form_date")); // 产生第3个单元格 處理時間 HSSFCell cell_03 = row.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); cell_03.setCellValue(new HSSFRichTextString("isTransmit")); HSSFCell newCell_01 = null; HSSFCell newCell_02 = null; HSSFCell newCell_03 = null; int s2sSize = 0; if (s2sList != null) { s2sSize = s2sList.size(); } S2s s = null; for (int i = 1; i < s2sSize; i++) { s = (S2s) s2sList.get(i); // 产生一行 HSSFRow rowNew = sheet.createRow((short) i); newCell_01 = rowNew.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); newCell_01.setCellValue(new HSSFRichTextString(s.getCust_id())); // 产生第2个单元格 BLK newCell_02 = rowNew.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); newCell_02 .setCellValue(new HSSFRichTextString(s.getForm_date())); // 产生第3个单元格 處理時間 newCell_03 = rowNew.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); newCell_03.setCellValue(new HSSFRichTextString(s .getIsTransmit())); } FileOutputStream fOut = new FileOutputStream(xlsFile); workbook.write(fOut); fOut.flush(); fOut.close(); System.out.println("文件生成..."); // 以下语句读取生成的Excel文件内容 FileInputStream fIn = new FileInputStream(xlsFile); HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn); HSSFSheet readSheet = readWorkBook.getSheet("Sheet1"); HSSFRow readRow = readSheet.getRow(0); HSSFCell readCell = readRow.getCell((short) 0); System.out.println("第一個單元是:" + readCell.getRichStringCellValue()); } catch (Exception e) { e.printStackTrace(); System.out.println(e); } } public static void createExcelFlag() { String xlsFile = "D:/0Excel/Flag.xls"; List flagList = readFlag(); try { // 产生工作簿对象 HSSFWorkbook workbook = new HSSFWorkbook(); // 产生工作表对象,设置工作表的名称为Sheet1 HSSFSheet sheet = workbook.createSheet("Sheet1"); sheet.setColumnWidth((short) 0, (short) 4000); sheet.setColumnWidth((short) 1, (short) 6000); sheet.setColumnWidth((short) 2, (short) 6000); // 产生一行 HSSFRow row = sheet.createRow((short) 0); // 产生第1个单元格 cust_id HSSFCell cell_01 = row.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); cell_01.setCellValue(new HSSFRichTextString("cust_id")); // 产生第2个单元格 startDate HSSFCell cell_02 = row.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); cell_02.setCellValue(new HSSFRichTextString("startDate")); // 产生第3个单元格 endDate HSSFCell cell_03 = row.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); cell_03.setCellValue(new HSSFRichTextString("endDate")); HSSFCell newCell_01 = null; HSSFCell newCell_02 = null; HSSFCell newCell_03 = null; int flagSize = 0; if (flagList != null) { flagSize = flagList.size(); } Flag f = null; for (int i = 1; i < flagSize; i++) { f = (Flag) flagList.get(i); // 产生一行 HSSFRow rowNew = sheet.createRow((short) i); // 产生第1个单元格 cust_id newCell_01 = rowNew.createCell((short) 0, HSSFCell.CELL_TYPE_STRING); newCell_01.setCellValue(new HSSFRichTextString(f .getCustomer_id())); // 产生第2个单元格 startDate newCell_02 = rowNew.createCell((short) 1, HSSFCell.CELL_TYPE_STRING); newCell_02 .setCellValue(new HSSFRichTextString(f.getStartDate())); // 产生第3个单元格 endDate newCell_03 = rowNew.createCell((short) 2, HSSFCell.CELL_TYPE_STRING); newCell_03.setCellValue(new HSSFRichTextString(f.getEndDate())); } FileOutputStream fOut = new FileOutputStream(xlsFile); workbook.write(fOut); fOut.flush(); fOut.close(); System.out.println("文件生成..."); // 以下语句读取生成的Excel文件内容 FileInputStream fIn = new FileInputStream(xlsFile); HSSFWorkbook readWorkBook = new HSSFWorkbook(fIn); HSSFSheet readSheet = readWorkBook.getSheet("Sheet1"); HSSFRow readRow = readSheet.getRow(0); HSSFCell readCell = readRow.getCell((short) 0); System.out.println("第一個單元是:" + readCell.getRichStringCellValue()); } catch (Exception e) { System.out.println(e); } } public static void testReplaceAll() { String startDate = "2008.09.01"; String endDate = "2008.09.30"; String sDate = startDate.replaceAll("[.]", ""); String eDate = endDate.replaceAll("[.]", ""); System.out.println(startDate + ":" + endDate + ":" + sDate + ":" + eDate); } }
运行结果:
custIdSet.size(): 20 beansMap.size(): 20 s2sList.size(): 10 true : false : S223821747: true : false : L123462919: false : false : F223700449: false : false : U120222840: true : false : F224800911: true : false : S223821747: true : true : Z100098584: ####6:true : true : Z100098584: false : false : L100867210: true : true : C221114157: ####8:true : true : C221114157: true : true : B122081672: ####9:true : true : B122081672: