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

 Deepak, Prasath
 Rakesh, Mehra
 Prakash, Merhra


现在,我想做的是:如果有人尝试添加Deepak Kumar之类的东西,例如“ Deepak,Kareem”。然后该脚本应该能够追加到现有记录,例如

 Deepak, Prasath, Kareem


有谁能帮忙,如何使用OpenCSV包将其附加到特定的行或列?

最佳答案

我编写了满足此要求的代码:

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

}

关于java - 使用OpenCSV查找列号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17878601/

10-12 03:05