如何使用搜索字符串从CSV文件中删除特定行

问题描述

我正在研究Java项目,必须使用Java从CSV文件中删除特定行。目前,我正在使用opencsv。我正在尝试实现以下情形,其中我必须从列表中删除第三行,并且有两个字符串作为输入。

字符串1:猫

字符串2:火星

enter image description here

我可以使用当前代码获取确切的行及其编号。如何删除该行?

这是我的代码:

private static void updateCsv(String string1,String String2) throws IOException {
    try {
        CSVReader reader = new CSVReader(new FileReader(OUTPUTFILE),',');
        List<String[]> myEntries = reader.readAll();
        reader.close();

        //Iterate through my array to find the row the user input is located on
        int i = 1;
        for (String[] line : myEntries) {
            String textLine = Arrays.toString(line).replaceAll("\\[|\\]","");
        
            //here i am checking for the two strings
            if (textLine.contains(string1) && textLine.contains(string2) ) {
                //here i am able to get the count the row as 3
                System.out.println("Found - Your item is on row: ...:" + i);
                // how can i delete the row that i have now ?
          
            } else {
                //System.out.println("Not found");
            }
            i++;
        }
    } catch (IOException e) {
        System.out.println(e);
    }
 }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)