Example usage for org.apache.commons.csv CSVParser close

List of usage examples for org.apache.commons.csv CSVParser close

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes resources.

Usage

From source file:com.chargebee.CSV.PhoneBook.PhoneBook2.PhoneBook.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input2.csv";

    PhoneBook pb = new PhoneBook();

    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());

    pb.directory(parser);/*from   ww w. ja v a  2  s .  c o m*/
    parser.close();

}

From source file:com.chargebee.CSV.RemovingDuplicateEntries.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input.csv";
    String output = System.getProperty("user.home") + "/output.csv";

    RemovingDuplicateEntries rde = new RemovingDuplicateEntries();

    CSVParser parser = MethodBank.parserInitializer(source);
    CSVPrinter printer = MethodBank.printerInitializer(output);

    rde.remove(parser, printer);//from   w w  w .  j  ava 2s  . c o  m
    parser.close();
    printer.close();

}

From source file:com.chargebee.Application.DateFormat.java

public static void main(String[] args) throws IOException, Exception {
    Scanner sc = new Scanner(System.in);
    System.out.println("Input CSV File: ");
    String source = sc.nextLine();

    System.out.println("Input JSON File: ");
    String requirements = sc.nextLine();

    System.out.println("Output CSV File: ");
    String output = sc.nextLine();

    DateFormat df = new DateFormat();
    JSONObject jobj = MethodBank.readJsonObjectData(requirements);
    CSVParser parser = MethodBank.parserInitializer(source);
    CSVPrinter printer = MethodBank.printerInitializer(output);

    df.formatter(parser, printer, jobj);
    parser.close();
    printer.close();//from   w  w w  .jav a 2  s. c om

}

From source file:com.chargebee.Application.Amountediting.java

public static void main(String[] args) throws IOException, Exception {
    Scanner sc = new Scanner(System.in);
    System.out.println("Input CSV File: ");
    String source = sc.nextLine();

    System.out.println("Input JSON File: ");
    String requirements = sc.nextLine();

    System.out.println("Output CSV File: ");
    String output = sc.nextLine();

    Amountediting ae = new Amountediting();
    JSONObject jobj = MethodBank.readJsonObjectData(requirements);
    CSVParser parser = MethodBank.parserInitializer(source);
    CSVPrinter printer = MethodBank.printerInitializer(output);

    ae.formatter(parser, printer, jobj);
    parser.close();
    printer.close();//w ww .  j av  a2 s .c  o  m

}

From source file:com.chargebee.JDBC.PhoneBook.Phnbk.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input.csv";
    com.chargebee.JDBC.PhoneBook.Phnbk pb = new com.chargebee.JDBC.PhoneBook.Phnbk();
    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());
    pb.directory(parser);/*from  www. ja va2  s.  co  m*/
    parser.close();

}

From source file:Phnbk.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input.csv";
    Phnbk pb = new Phnbk();
    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());
    pb.directory(parser);//w w  w.  ja v  a2 s.c o m
    parser.close();

}

From source file:com.chargebee.Application.MappingHeaders.java

public static void main(String[] args) throws Exception {
    String source1 = System.getProperty("user.home") + "/Output-3.csv"; // Source CSV file containing the customer details 
    String source2 = System.getProperty("user.home") + "/header.json";//Json file containing the customer id and token
    String output = System.getProperty("user.home") + "/Output-4.csv";// The destination CSV file.
    //        Scanner sc = new Scanner(System.in);
    //        System.out.println("Input CSV File: ");
    //        String source1 = sc.nextLine();
    ////from ww w. j  a  v a 2 s  .  co  m
    //        System.out.println("config File: ");
    //        String source2 = sc.nextLine();
    //
    //        System.out.println("Output CSV File: ");
    //        String output = sc.nextLine();
    //        

    MappingHeaders objHm = new MappingHeaders();
    JSONObject jobj = objHm.readJsonData(source2);
    CSVPrinter printer = new CSVPrinter(new FileWriter(output),
            CSVFormat.EXCEL.withRecordSeparator("\n").withDelimiter(','));
    CSVParser parser = new CSVParser(new FileReader(source1), CSVFormat.EXCEL.withHeader());
    objHm.extractJsonData(jobj, printer, parser);
    parser.close();
    printer.close();

}

From source file:formats.db.fileformats.csv.RunnerCsv.java

public static void main(String[] args) throws IOException {

    CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',');

    //initialize the CSVParser object

    CSVParser parser = new CSVParser(new FileReader("D:\\customers.csv"), format);

    List<Customers> customersList = new ArrayList<Customers>();
    for (CSVRecord record : parser) {
        Customers customers = new Customers();

        customers.setId_cs(Integer.parseInt(record.get("id_cs")));
        customers.setF_name(record.get("f_name"));
        customers.setL_name(record.get("l_name"));
        customers.setDiscount(Integer.parseInt(record.get("discount")));
        customers.setLicense(record.get("license"));

        customersList.add(customers);/*  w w  w. j a v a 2  s .  c om*/
    }
    //close the parser
    parser.close();

    System.out.println(customersList);
}

From source file:edu.harvard.liblab.ecru.LoadCsvData.java

/**
 * @param args// w  w  w  .j a v  a 2s . c o m
 */
public static void main(String[] args) {
    if (args.length > 7 | args.length == 0 || !args[0].equals("-f") || !args[2].equals("-u")
            || !args[4].equals("-i")) {
        System.err.println(USAGE);
        System.exit(1);
    }
    String filename = args[1].trim();
    url = args[3].trim();
    needsPrefix = !args[5].equals("unique");
    isVerbose = (args.length == 7 && args[6].equals("-v"));
    System.out.println("Loading data from " + filename + " " + (needsPrefix ? "IDs will be prefixed " : " "));
    long start = System.currentTimeMillis();
    boolean isReading = false;
    CSVPrinter printer = null;

    CSVFormat format = CSVFormat.EXCEL.withHeader().withDelimiter(',').withAllowMissingColumnNames(true);
    CSVParser parser;
    try {
        if (isVerbose) {
            printer = new CSVPrinter(System.err, format.withDelimiter('|'));
        }
        parser = CSVParser.parse(new File(filename), Charset.forName("UTF-8"), format);

        solrSrvr = SingletonSolrServer.getSolrServer(url);
        for (CSVRecord record : parser) {
            numRecs++;
            HashMap<String, String> recMap = new HashMap<String, String>();
            for (String field : FIELDS) {
                String value = null;
                try {
                    value = record.get(field);
                } catch (IllegalArgumentException e) {
                    if (e.getMessage().indexOf("expected one of") == -1) {
                        e.printStackTrace();
                        System.exit(1);
                    }
                }
                value = value == null ? "" : value.trim();
                recMap.put(field, value);
            }
            String id = recMap.get("ID");
            if (id.isEmpty()) {
                if (isVerbose) {
                    System.err.println("Record missing ID: ");
                    printer.printRecord(record);
                }
            } else {
                String type = recMap.get("Type");
                SolrDocument sdoc = getDocFromSolr(recMap.get("ID"));
                try {
                    if (type.toLowerCase().equals("course")) {
                        processCourse(recMap, sdoc);
                        isReading = false;
                    } else {
                        if (!isReading) {
                            addUpdateCommit(); // just in case the preceeding course(s) are related
                        }
                        processReading(recMap, sdoc);
                        isReading = true;
                    }
                } catch (Exception e) {
                    if (isVerbose) {
                        System.err.println("Record # " + numRecs + " not used:\n\t" + e.getMessage());
                    }
                    errRecs++;
                }
            }
            if (beans.size() > 20) {
                addUpdateCommit();
            }
        }
        parser.close();
        if (beans.size() > 0 || docUpdates.size() > 0) {
            addUpdateCommit();
        }
    } catch (FileNotFoundException e) {
        System.err.println(filename + " not found");
        System.exit(1);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    long end = System.currentTimeMillis();
    long courseTime = (end - start) / (long) 1000;
    try {
        solrSrvr.optimize();
    } catch (SolrServerException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    System.out.println(numRecs + " records found, of which " + errRecs + " had a problem; time: " + courseTime
            + " seconds " + ((courseTime > 60) ? ("(" + (courseTime / (long) 60) + " minutes)") : ""));
    System.exit(0);
}

From source file:de.upb.wdqa.wdvd.geolocation.GeolocationDatabase.java

/**
 * Reads the csv file of the TagDownloader
 *///from  w  w  w .j a v  a  2  s  . c  o  m
public static void readFile(File file) {
    try {
        logger.info("Starting to read file of GeolocationDatabase ...");
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file))), "UTF-8"));

        CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180);

        for (CSVRecord csvRecord : parser) {
            parseRecord(csvRecord);
            if (csvRecord.getRecordNumber() % 1000000 == 0) {
                logger.info("Current Record: " + csvRecord.getRecordNumber());
            }
        }

        parser.close();
        logger.info("Finished");
    } catch (Exception e) {
        logger.error("", e);
    }
}