0

我在 Java 中使用 openCSV 包来创建 CSV。我正在创建一个包含两列的 CSV。第一列代表一个人的名字。第二列代表一个人的第二个名字。所以,就像

 Deepak, Prasath
 Rakesh, Mehra
 Prakash, Merhra

现在,我想做的是:如果有人尝试像 "Deepak,Kareem" 一样添加 Deepak Kumar。然后脚本应该能够附加到现有记录,如

 Deepak, Prasath, Kareem

任何人都可以帮助,我们如何使用 OpenCSV 包附加到特定的行或列?

4

2 回答 2

0

您需要遍历文档并找到 Deepak 所在的行。然后你从它旁边的单元格中获取文本并将你的“,Kareem”附加到它,然后将文本保存到单元格中。

于 2013-07-26T10:12:02.823 回答
0

我制作了符合此要求的代码:

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);
    }

}
于 2013-08-02T09:21:42.980 回答