相关文章推荐
怕老婆的匕首  ·  Java ...·  2 小时前    · 
有胆有识的玉米  ·  jdk8 ...·  2 小时前    · 
痴情的毛衣  ·  list分组合并成新的List ...·  2 小时前    · 
魁梧的小蝌蚪  ·  C# - ...·  1 月前    · 
温暖的八宝粥  ·  为什么 Python 的 GIL ...·  1 年前    · 
宽容的消炎药  ·  c++ - What does it ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a CSV file which looks like this: http://gyazo.com/5dcfb8eca4e133cbeac87f514099e320.png

I need to figure out how I can read specific cells and update them in the file.

This is the code I am using:

import java.util.List;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.opencsv.*;
public class ReadCSV {
    private static final char SEPARATOR = ';';
    public static void updateCSV(String input, String output, String  replace, int row, int col) throws IOException {   
          CSVReader reader = new CSVReader(new FileReader(input),SEPARATOR);
            List<String[]> csvBody = reader.readAll();
            csvBody.get(row)[col]=replace;
            reader.close();
            CSVWriter writer = new CSVWriter(new FileWriter(output),SEPARATOR,' ');
            writer.writeAll(csvBody);
            writer.flush();
            writer.close();
    public static void main(String[] args) throws IOException {
        String source = "townhall_levels.csv";
        String destiantion="output.csv";
        ReadCSV.updateCSV(source, destiantion, "lol", 1, 1);

In this code I am just trying to change A1 to "lol" as an example test to see if it works but I get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at ReadCSV.updateCSV(ReadCSV.java:16)
    at ReadCSV.main(ReadCSV.java:30)

How should I go about achieving my goal and fixing the error?

CSV File: www.forumalliance.net/townhall_levels.csv

Post your csv file as text, not as a link to an image. We need to see what is actually in the file. You should also learn to debug by yourself: print each array of the body, or use your debugger to see what it contains. – JB Nizet Jun 2, 2015 at 16:23 thanks, that doesn't seem to have worked entirely though: gyazo.com/a7aedf1112615d5c951c0acb5aae7480 – Shivam Paw Jun 2, 2015 at 16:23 You should debug your code to see that you're actually getting values that you expect. If you're not comfortable using a debugger (which I would recommend), even some System.out.println() statements will help you see what the problem might be – David Koelle Jun 2, 2015 at 16:25

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.