5

我正在阅读 2 个 csv 文件:store_inventory& new_acquisitions
我希望能够将store_inventorycsv 文件与new_acquisitions. 1) 如果项目名称匹配,只需更新 store_inventory 中的数量。2) 如果 new_acquisitions 有一个在 中不存在的新项目store_inventory,则将其添加到store_inventory.

这是我到目前为止所做的,但它不是很好。我在需要添加 taks 12的地方添加了评论。
任何完成上述任务的建议或代码都会很棒!谢谢。

    File new_acq = new File("/src/test/new_acquisitions.csv");
    Scanner acq_scan = null;
    try {
        acq_scan = new Scanner(new_acq);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
    }
    String itemName;
    int quantity;
    Double cost;
    Double price;

    File store_inv = new File("/src/test/store_inventory.csv");
    Scanner invscan = null;
    try {
        invscan = new Scanner(store_inv);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
    }
    String itemNameInv;
    int quantityInv;
    Double costInv;
    Double priceInv;


    while (acq_scan.hasNext()) {
        String line = acq_scan.nextLine();
        if (line.charAt(0) == '#') {
            continue;
        }
        String[] split = line.split(",");

        itemName = split[0];
        quantity = Integer.parseInt(split[1]);
        cost = Double.parseDouble(split[2]);
        price = Double.parseDouble(split[3]);


        while(invscan.hasNext()) {
            String line2 = invscan.nextLine();
            if (line2.charAt(0) == '#') {
                continue;
            }
            String[] split2 = line2.split(",");

            itemNameInv = split2[0];
            quantityInv = Integer.parseInt(split2[1]);
            costInv = Double.parseDouble(split2[2]);
            priceInv = Double.parseDouble(split2[3]);


            if(itemName == itemNameInv) {
                //update quantity

            }
        }
        //add new entry into csv file

     }

再次感谢任何帮助。=]

4

7 回答 7

5

建议您使用现有的 CSV 解析器之一,例如Commons CSVSuper CSV,而不是重新发明轮子。应该会让你的生活轻松很多。

于 2010-06-06T02:09:51.863 回答
3

您的实现犯了一个常见错误,即使用line.split(","). 这不起作用,因为值本身可能包含逗号。如果发生这种情况,则必须引用该值,并且您需要忽略引号内的逗号。该split方法不能做到这一点——我经常看到这个错误。

这是正确执行的实现的来源:http: //agiletribe.purplehillsbooks.com/2012/11/23/the-only-class-you-need-for-csv-files/

于 2012-11-23T19:38:06.343 回答
2

在开源库uniVocity-parsers的帮助下,您可以使用非常干净的代码进行开发,如下所示:

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);
        }
    }
}

只需按照我根据您的要求制定的代码示例即可。请注意,该库为解析 CSV 文件提供了简化的 API 和显着的性能。

于 2015-05-10T15:12:02.857 回答
1

您正在执行的操作将要求对于新收购中的每个项目,您将需要搜索库存中的每个项目以查找匹配项。这不仅效率不高,而且您为库存文件设置的扫描仪需要在每个项目后重置。

我建议您将新收购和库存添加到收藏中,然后遍历新收购并在库存收藏中查找新项目。如果该项目存在,则更新该项目。如果没有,请将其添加到库存集合中。对于这个活动,最好编写一个简单的类来包含库存项目。它可以用于新的收购和库存。为了快速查找,我建议您使用 HashSet 或 HashMap 来收集库存。

在该过程结束时,不要忘记将更改保存到您的库存文件中。

于 2010-06-06T02:15:07.990 回答
1

由于 Java 本身不支持解析 CSV 文件,因此我们必须依赖第三方库。Opencsv是可用于此目的的最佳库之一。它是开源的,附带 Apache 2.0 许可证,可以用于商业用途。

在这里,这个链接应该可以帮助您和其他人在这种情况下!

于 2012-12-22T12:29:48.457 回答
0

用于写入 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();
            }
        }
    }
于 2016-12-12T09:58:48.017 回答
0

您可以使用 Apache Commons CSV api。仅供参考:https ://stackoverflow.com/a/42198895/6549532

读/写示例

于 2017-02-13T07:50:41.230 回答