java - Parsing CSV file with BufferedReader vs Scanner -
team, have parse file line line , in single line have split ",". first string name , second count. finaly have display key , count example
peter,2 smith,3 peter,3 smith,5
i should display peter 5 , smith 8.
so in confusion choose between bufferedreader vs scanner. went through link . came these 2 approach. concerns.
approach 1 : use buffered reader.
private hashmap<string, mutablelong> readfile(file file) throws ioexception { final hashmap<string, mutablelong> keyholder = new hashmap<>(); try (bufferedreader br = new bufferedreader(new inputstreamreader( new fileinputstream(file), "utf-8"))) { (string line; (line = br.readline()) != null;) { // processing line. final string[] keycontents = line .split(keycountexam.comma_delimeter); if (keycontents.length == 2) { final string keyname = keycontents[0]; final long count = long.parselong(keycontents[1]); final mutablelong keycount = keyholder.get(keyname); if (keycount != null) { keycount.add(count); keyholder.put(keyname, keycount); } else { keyholder.put(keyname, new mutablelong(count)); } } } } return keyholder; } private static final string comma_delimeter = ","; private static volatile pattern commapattern = pattern .compile(comma_delimeter);
i have used mutablelong ( , since dont want create biginteger each time . , again may big file , don't have control on how max key can occur
another approach :
use scanner , use 2 delimiter
private static final string line_separator_pattern = "\r\n|[\n\r\u2028\u2029\u0085]"; private static final string line_pattern = ".*(" + line_separator_pattern + ")|.+$"; private static volatile pattern linepattern = pattern.compile(line_pattern);
my question . have went through hasnext in scanner , me there no harm switch pattern . , belive java 7, scanner has limited buffer can enough kind of file.
do 1 perfer approach 2 on approach 1 or have other option other this. did sop testing purpose. same code in approach 1 replace here. using split in approach1 create multiple string instances. can avoided here ( right) , scanning char sequence.
private hashmap<string, biginteger> readfilescanner(file file) throws ioexception { final hashmap<string, biginteger> keyholder = new hashmap<>(); try (scanner br = new scanner(file, "utf-8")) { while (br.hasnext()) { br.usedelimiter(commapattern); system.out.println(br.next()); system.out.println(br.next()); br.usedelimiter(linepattern); } } return keyholder; }
Comments
Post a Comment