Find a line in a file and remove it

后端 未结 16 1885
谎友^
谎友^ 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

        public static void deleteLine() throws IOException {
            RandomAccessFile file = new RandomAccessFile("me.txt", "rw");
            String delete;
            String task="";
            byte []tasking;
            while ((delete = file.readLine()) != null) {
                if (delete.startsWith("BAD")) {
                    continue;
                }
                task+=delete+"\n";
            }
            System.out.println(task);
            BufferedWriter writer = new BufferedWriter(new FileWriter("me.txt"));
            writer.write(task);
            file.close();
            writer.close();
        }
    

提交回复
热议问题