Java: CSV file read & write

前端 未结 7 1517
南旧
南旧 2020-12-10 08:18

I\'m reading 2 csv files: store_inventory & new_acquisitions.
I want to be able to compare the store_inventory csv file with <

相关标签:
7条回答
  • 2020-12-10 08:30

    As Java doesn’t support parsing of CSV files natively, we have to rely on third party library. Opencsv is one of the best library available for this purpose. It’s open source and is shipped with Apache 2.0 licence which makes it possible for commercial use.

    Here, this link should help you and others in the situations!

    0 讨论(0)
  • 2020-12-10 08:32

    For writing to CSV

    public void writeCSV() {
    
            // Delimiter used in CSV file
            private static final String NEW_LINE_SEPARATOR = "\n";
    
            // CSV file header
            private static final Object[] FILE_HEADER = { "Empoyee Name","Empoyee Code", "In Time", "Out Time", "Duration", "Is Working Day" };
    
            String fileName = "fileName.csv");
            List<Objects> objects = new ArrayList<Objects>();
            FileWriter fileWriter = null;
            CSVPrinter csvFilePrinter = null;
    
            // Create the CSVFormat object with "\n" as a record delimiter
            CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
    
            try {
                fileWriter = new FileWriter(fileName);
    
                csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
    
                csvFilePrinter.printRecord(FILE_HEADER);
    
                // Write a new student object list to the CSV file
                for (Object object : objects) {
                    List<String> record = new ArrayList<String>();
    
                    record.add(object.getValue1().toString());
                    record.add(object.getValue2().toString());
                    record.add(object.getValue3().toString());
    
                    csvFilePrinter.printRecord(record);
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    fileWriter.flush();
                    fileWriter.close();
                    csvFilePrinter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-10 08:34

    The operation you are performing will require that for each item in your new acquisitions, you will need to search each item in inventory for a match. This is not only not efficient, but the scanner that you have set up for your inventory file would need to be reset after each item.

    I would suggest that you add your new acquisitions and your inventory to collections and then iterate over your new acquisitions and look up the new item in your inventory collection. If the item exists, update the item. If it doesnt, add it to the inventory collection. For this activity, it might be good to write a simple class to contain an inventory item. It could be used for both the new acquisitions and for the inventory. For a fast lookup, I would suggest that you use HashSet or HashMap for your inventory collection.

    At the end of the process, dont forget to persist the changes to your inventory file.

    0 讨论(0)
  • 2020-12-10 08:42

    With help of the open source library uniVocity-parsers, you could develop with pretty clean code as following:

    private void processInventory() throws IOException {
        /**
         * ---------------------------------------------
         *  Read CSV rows into list of beans you defined
         * ---------------------------------------------
         */
        // 1st, config the CSV reader with row processor attaching the bean definition
        CsvParserSettings settings = new CsvParserSettings();
        settings.getFormat().setLineSeparator("\n");
        BeanListProcessor<Inventory> rowProcessor = new BeanListProcessor<Inventory>(Inventory.class);
        settings.setRowProcessor(rowProcessor);
        settings.setHeaderExtractionEnabled(true);
    
        // 2nd, parse all rows from the CSV file into the list of beans you defined
        CsvParser parser = new CsvParser(settings);
        parser.parse(new FileReader("/src/test/store_inventory.csv"));
        List<Inventory> storeInvList = rowProcessor.getBeans();
        Iterator<Inventory> storeInvIterator = storeInvList.iterator();
    
        parser.parse(new FileReader("/src/test/new_acquisitions.csv"));
        List<Inventory> newAcqList = rowProcessor.getBeans();
        Iterator<Inventory> newAcqIterator = newAcqList.iterator();
    
        // 3rd, process the beans with business logic
        while (newAcqIterator.hasNext()) {
    
            Inventory newAcq = newAcqIterator.next();
            boolean isItemIncluded = false;
            while (storeInvIterator.hasNext()) {
                Inventory storeInv = storeInvIterator.next();
    
                // 1) If the item names match just update the quantity in store_inventory
                if (storeInv.getItemName().equalsIgnoreCase(newAcq.getItemName())) {
                    storeInv.setQuantity(newAcq.getQuantity());
                    isItemIncluded = true;
                }
            }
    
            // 2) If new_acquisitions has a new item that does not exist in store_inventory,
            // then add it to the store_inventory.
            if (!isItemIncluded) {
                storeInvList.add(newAcq);
            }
        }
    }
    

    Just follow this code sample I worked out according to your requirements. Note that the library provided simplified API and significent performance for parsing CSV files.

    0 讨论(0)
  • 2020-12-10 08:44

    You can use Apache Commons CSV api. FYI this anwser : https://stackoverflow.com/a/42198895/6549532

    Read / Write Example

    0 讨论(0)
  • 2020-12-10 08:50

    Your implementation makes the common mistake of breaking the line on commas by using line.split(","). This does not work because the values themselves might have commas in them. If that happens, the value must be quoted, and you need to ignore commas within the quotes. The split method can not do this -- I see this mistake a lot.

    Here is the source of an implementation that does it correctly: http://agiletribe.wordpress.com/2012/11/23/the-only-class-you-need-for-csv-files/

    0 讨论(0)
提交回复
热议问题