使用java更新特定单元csv文件 [英] updating specific cell csv file using java

查看:235
本文介绍了使用java更新特定单元csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个小问题,并认为我只是没有得到正确的语法在一行代码。基本上,我可以写入我的csv文件,并使用字符串tokenizer查找特定的记录,但它不更新/编辑该记录的指定单元格。记录保持不变。请帮助....

Hi i have a small problem and think i'm just not getting the correct syntax on one line of code. basically, i can write into my csv file and find a specific record using string tokenizer but it is not updating/editing the specified cells of that record. the record remains the same. please help....

推荐答案

我使用了 http://opencsv.sourceforge.net


您好,
这是通过指定行和列来更新CSV的代码

Hi, This is the code to update CSV by specifying row and column



/**
 * Update CSV by row and column
 * 
 * @param fileToUpdate CSV file path to update e.g. D:\\chetan\\test.csv
 * @param replace Replacement for your cell value
 * @param row Row for which need to update 
 * @param col Column for which you need to update
 * @throws IOException
 */
public static void updateCSV(String fileToUpdate, String replace,
    int row, int col) throws IOException {

File inputFile = new File(fileToUpdate);

// Read existing file 
CSVReader reader = new CSVReader(new FileReader(inputFile), ',');
List<String[]> csvBody = reader.readAll();
// get CSV row column  and replace with by using row and column
csvBody.get(row)[col] = replace;
reader.close();

// Write to CSV file which is open
CSVWriter writer = new CSVWriter(new FileWriter(inputFile), ',');
writer.writeAll(csvBody);
writer.flush();
writer.close();
}



此解决方案适用于我,
Cheers!

This solution worked for me, Cheers!

这篇关于使用java更新特定单元csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆