java - what is the most efficient way to extract rows from one CSV file using input from another CSV file -
i have 2 csv files input1.txt
, input2.txt
. need take 1 value input1.txt
, search in input2.txt
, if found, write entire record input2.txt
file.
i have written below code , working slow. there more efficient way achieve this?
import java.io.*; public class filecompare { private static final eol = system.getproperty("line.separator"); public static void main(string args[]) throws ioexception { filereader in = null; filereader comp = null; filewriter out = null; filewriter out2 = null; try { in = new filereader("input1.txt"); //file containing value search out = new filewriter("matching.txt"); // file write output out2 = new filewriter("notfound.txt"); // file write non matching bufferedreader br = new bufferedreader(in); string p, q; boolean done; // read input file line line while ((p = br.readline()) != null) { done = false; string str = p; string delims = "[\\t]"; string[] tokens = str.split(delims); long acno = long.parselong(tokens[5]); // value in input1.txt checked against input2.txt long si = long.parselong(tokens[31]); comp = new filereader ("input2.txt"); // file containing bulk data bufferedreader bbr = new bufferedreader (comp); // opening bulk file while (!done) { // flag break second file operation if matching value found while ((q = bbr.readline()) != null) { // loop read bulk file string delim2 = "[\\t]"; string[] token2 = q.split(delim2); // splitting file based on delimiter long acno1 = long.parselong(token2[5]); long si1 = long.parselong(token2[31]); if (acno1.equals(acno)) { // checking condition out.write(q + eol); // writing matching entry(entire row) input2.txt done = true; // changing flag value , control go input file next line break; // breaking bulk reading loop , because got value per condition } else { // if condition not satisfies out2.write(p + eol); //writing entry input1.txt if not matching done = true; // flag value changing , control go input file next } } } } } { if (in != null) in.close(); if (out != null) out.close(); if (out2 != null) out2.close(); } } }
Comments
Post a Comment