java - delete Row in a CSV file -
this question has answer here:
- how delete rows of text file? 6 answers
- remove duplicate rows csv file without write new file 2 answers
delete rows based on duplicate column in csv file eg., csv file contains.
campaigncode | cellcode | startdate | phone| account c000001413 | a000363363 | 20170601 | 4167292999 | 999999999 c000001414 | a000363364 | 20170601 | 4167292999 | 999999999
here want delete complete row based on phone
column duplicates. expected output is: campaigncode | cellcode | startdate | phone | account c000001413 | a000363363 | 20170601 | 4167292999 | 999999999
solution read csv file line line, put each line list , check duplicate @ column want => write down list of new line current file. sample code (i'm using comma seperate instead of |)
public class csvreader { public static void main(string[] args) throws ioexception { string csvfile = "csv.csv"; bufferedreader br = null; string line = ""; string cvssplitby = ","; list<string> lines = new arraylist<string>(); boolean isduplicate = false; br = new bufferedreader(new filereader(csvfile)); while ((line = br.readline()) != null) { isduplicate = false; string[] object = line.split(cvssplitby); (string l : lines) { if (l.contains(object[3])) { isduplicate = true; break; } } if (!isduplicate) { lines.add(line); } } (string newl : lines) { system.out.println(newl); } br.close(); } }
Comments
Post a Comment