使用java更新特定的单元格csv文件

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

我在java中使用过http://opencsv.sourceforge.net

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

/** * Update CSV by row and column * * @param fileToUpdate CSV file path to update eg 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 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(); } 

这个解决方案对我有用,干杯!

你正在做这样的事情:

  String line = readLineFromFile(); line.replace(...); 

这不是编辑文件,而是从文件中的一行创建一个新字符串

String实例是不可变的,因此您正在进行的replace调用返回一个字符串,它不会修改原始字符串。

使用允许您同时读取和写入文件的文件流 – 即RandomAccessFile或(更简单地)写入新文件,然后用新文件替换旧文件

在伪代码中:

 for (String line : inputFile) { String [] processedLine = processLine(line); outputFile.writeLine(join(processedLine, ",")); } private String[] processLine(String line) { String [] cells = line.split(","); // note this is not sufficient for correct csv parsing. for (int i = 0; i < cells.length; i++) { if (wantToEditCell(cells[i])) { cells[i] = "new cell value"; } } return cells; } 

另外,请看一下这个问题 。 有些库可以帮助您处理csv。

CSV文件只是一个文件。 如果你正在阅读它,它不会被改变。 所以,写下你的改变!

你有3种方法。

1

  1. 逐行读取您要更改的单元格。
  2. 如果需要更改单元格并合并当前行的新版本。
  3. 将该行写入第二个文件。
  4. 完成后,您将拥有源文件和结果文件。 现在,如果需要,可以删除源文件并将结果文件重命名为source。

2

使用RandomAccess文件写入文件的特定位置。

3

使用CSV解析器的可用实现之一(例如http://commons.apache.org/sandbox/csv/ )它已经支持您需要的内容并公开高级API。

好的,我正在做这样的事情。 我可以访问令牌指向的csv记录,但它不会更新我认为不正确的语法

 private static void ChangeRecord(String sFileName) { // TODO Auto-generated method stub try { BufferedReader r = new BufferedReader( new InputStreamReader( new FileInputStream(sFileName))); String line; while((line = r.readLine()) != null){ String tokens[] = line.split(","); String Surname = tokens[1]; String network= tokens[2]; String city= tokens[4]; StringTokenizer StrTok = new StringTokenizer(party); if(StrTok.hasMoreTokens()){ if ((city.equals("New York"))&&(Surname.equals("Smith"))){ String Lovely= "waterloo"; //this line is the one that i think is failing line.replace(network, Lovely); System.out.println(line); } } } r.close(); } catch(IOException e) { System.out.println(" There was an Error Reading the file. " + e.getMessage()); } 

我使用下面的代码,我将替换另一个字符串,它完全按照我需要的方式工作:

 public static void updateCSV(String fileToUpdate) throws IOException { File inputFile = new File(fileToUpdate); // Read existing file CSVReader reader = new CSVReader(new FileReader(inputFile), ','); List csvBody = reader.readAll(); // get CSV row column and replace with by using row and column for(int i=0; i