Question

I am using openCSV package in Java to create CSVs. I am creating a CSV with two columns. First Column represents first name of a person. Second Column represents second name of a person. So, it is like

 Deepak, Prasath
 Rakesh, Mehra
 Prakash, Merhra

Now, what I want to do is : If someone tries to add Deepak Kumar like "Deepak,Kareem" . Then the script should be able to append to the existing record like

 Deepak, Prasath, Kareem

Can any one help, how using OpenCSV package we can append to a particular row or column ?

Was it helpful?

Solution 2

I have made a code which meets this requirement :

static CSVReader read = null;
static CSVWriter writer = null;
static String csvfile;
static List<String[]> arg = null;
static Scanner in = new Scanner(System.in);

public static List<String[]> readFile(CSVReader read, List<String[]> arg) throws IOException {
    arg = read.readAll();

    for (String[] lineTokens : arg) {
        for (String token : lineTokens) {
            System.out.print(token + ",");
        }
        System.out.println("");
    }
    return arg;
}

public static void write_to_csv(String command_input, String command_output, CSVWriter writer, String csvfile, List<String[]> arg) throws IOException {
    String[] count = (command_input + "," + command_output).split(",");
    String rename = (getFilename(csvfile) + "_part.csv");
    int i = 0;
    String[] tail;
    writer = new CSVWriter(new FileWriter(rename));
    for (String[] head : arg) {
        if (head[0].equals(command_input)) {

            i = 0;
            tail = new String[head.length + 1];
            for (String h : head) {
                tail[i] = h;
                System.out.println(h);
                i++;
            }
            tail[tail.length - 1] = command_output;
            writer.writeNext(tail);
        } else {
            i--;
            writer.writeNext(head);
        }
    }
    if(i < 0){
        writer.writeNext(count);
    }
    writer.close();
    File original = new File(rename);
    File gre = new File(csvfile);
    boolean fr = original.renameTo(gre);
    System.out.println(fr);
}

static {
    try {
        System.out.println("Enter the file name");
        csvfile = in.nextLine();
        File file_exists = new File(csvfile);
        if (file_exists.exists()) {
            read = new CSVReader(new FileReader(csvfile));
        } else {
            file_exists.createNewFile();
            read = new CSVReader(new FileReader(csvfile));

        }
        arg = readFile(read, arg);
        read.close();
        if (file_exists.delete()) {
            System.out.println(file_exists.getName() + " is deleted.");
        } else {
            System.out.println("Operation Failed");
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(DifferentApproach.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(DifferentApproach.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static String getFilename(String csvfile) {
    String last[] = csvfile.split("\\.");//csvfile.split(".*(?=\\.)");
    return last[last.length - 2];
}

public static void main(String... args) {
    try {
        write_to_csv("mno", "Hi I am N Deepak Prasath", writer, csvfile, arg);
    } catch (IOException ex) {
        Logger.getLogger(DifferentApproach.class.getName()).log(Level.SEVERE, null, ex);
    }

}

OTHER TIPS

You need to iterate through the document and find the row where Deepak is. Then you get the text from the cell next to it and append your ",Kareem" to it, save the text into the cell afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top