Find a line in a file and remove it

后端 未结 16 1725
谎友^
谎友^ 2020-11-22 13:06

I\'m looking for a small code snippet that will find a line in file and remove that line (not content but line) but could not find. So for example I have in a file following

16条回答
  •  太阳男子
    2020-11-22 13:39

    Try this:

    public static void main(String[] args) throws IOException {
    
        File file = new File("file.csv");
    
        CSVReader csvFileReader = new CSVReader(new FileReader(file));
    
        List list = csvFileReader.readAll();
    
        for (int i = 0; i < list.size(); i++) {
            String[] filter = list.get(i);
            if (filter[0].equalsIgnoreCase("bbb")) {
                list.remove(i);
            }
        }
        csvFileReader.close();
        CSVWriter csvOutput = new CSVWriter(new FileWriter(file));
    
        csvOutput.writeAll(list);
        csvOutput.flush();
    
        csvOutput.close();
    }
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题