java 写csv文件_Java:读写CSV文件

我正在读取2个csv文件:store_inventory&new_acquisitions。

我希望能够将store_inventoryCSV文件与进行比较new_acquisitions。1)如果项目名称匹配,只需更新store_inventory中的数量。2)如果new_acquisitions具有不存在的新项目store_inventory,则将其添加到中store_inventory。

这是我到目前为止所做的,但是效果不是很好。我在需要添加taks 1 和 2的 地方添加了注释。

任何建议或执行上述任务的代码都将非常有用!谢谢。

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

}

再次感谢任何帮助。=]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值