现在有这么一个问题:
Name,CardNo,Descriot,CtfTp,CtfId,Gender,Birthday,Address
彬,,,ID,xxxxx21059,M,20101001,,,F,,CHN,,,,,,,,139,19:46:02,4000000
珊,,,ID,xxxxx811090189,M,20101001,,,F,,CHN,,,,,,,,158
世,,,ID,4xxxxx1772,M,19880129,,,F,,CHN,,,,,,,,152108,2,1,,,,,0,2010-10-19
超,,,ID,4xxxxx1772,M,20101001,,,F,,CHN,,,,,,,,1585091,,,mc06o.cn,,,,,,,,0,2010-10-19 19:46:04,4000003
磊,,,ID,4xxxxx1772,M,19831011,,,F,,CHN,,,,,,,,159159,362,,565408,,,,,,,,0,2010-10-19 19:46:05,4000004
芳,,,ID,xxxxx811090189,M,20101001,,,F,,CHN,,,,,,,,1506:05,4005
读取文件后,我要根据CtfTp 字段对应 的字段,删除掉重复的。
比如4xxxxx1772 有几个重复的,我要根据CtfTp将重复的删掉。
------解决方案--------------------
发现用Excel写代码也不错,至少对的起
import java.io.*;
import java.util.*;
public class TestMain {
public static void main(String[] args) {
ArrayList<HashMap<String, String>> rowList = readCsv("c:\\csv.csv");
ArrayList<HashMap<String, String>> distinctRowList = new ArrayList<HashMap<String,String>>();
HashMap<String, HashMap<String, String>> keyRowList = new HashMap<String, HashMap<String,String>>();
for (Iterator<HashMap<String, String>> iterator = rowList.iterator(); iterator.hasNext();) {
HashMap<String, String> currentRow = iterator.next();
try{
keyRowList.put(currentRow.get("CtfTp"), currentRow);
}catch(Exception e){
}
}
for (HashMap<String, String> hashMap : keyRowList.values()) {
distinctRowList.add(hashMap);
}
writeCsv(distinctRowList, "c:\\csv.csv");
}
private static ArrayList<HashMap<String, String>> readCsv(String fileName) {
ArrayList<HashMap<String, String>> retList = new ArrayList<HashMap<String,String>>();
String[] keys = new String[8];
File csv = null;
BufferedReader br = null;
try {
csv = new File(fileName);
br = new BufferedReader(new FileReader(csv));
String line = "";
int lineIndex = 0;
while ((line = br.readLine()) != null) {
String[] cells = line.split(",");
if (lineIndex > 0) {
HashMap<String, String> row = new HashMap<String, String>();
for (int i = 0; i < keys.length; i++) {
row.put(keys[i], cells[i]);
}
retList.add(row);
} else {
for (int i = 0; i < keys.length; i++) {
keys[i]=cells[i];
}
}
lineIndex++;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return retList;
}
private static void writeCsv(ArrayList<HashMap<String, String>> data, String fileName){
File csv = null;
BufferedWriter bw = null;
try {
csv = new File(fileName);
if (csv.exists()) {
csv.delete();
}
bw = new BufferedWriter(new FileWriter(csv, false));
bw.newLine();
bw.write("Name,CardNo,Descriot,CtfTp,CtfId,Gender,Birthday,Address");
for (Iterator<HashMap<String, String>> iterator = data.iterator(); iterator
.hasNext();) {
HashMap<String, String> currentRow = iterator.next();
bw.newLine();
bw.write(String.format("%s,%s,%s,%s,%s,%s,%s,%s",
currentRow.get("Name"), currentRow.get("CardNo"),
currentRow.get("Descriot"), currentRow.get("CtfTp"),
currentRow.get("CtfId"), currentRow.get("Gender"),
currentRow.get("Birthday"), currentRow.get("Address")));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bw !=null){
try {