在数据末尾找到参数时,如何删除所选行?

时间:2019-01-22 08:53:16

标签: java eclipse bufferedreader printwriter bufferedwriter

我的文本文件如下:

  

a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a 〜1   b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜b〜2   c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜3 < / p>

我的计划是在行尾找到数字,然后 删除该行。

我已经可以使用Delimeter找到该号码。 但是我不知道如何删除整行。

例如,我搜索2号,我希望输出如下:

  

a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a 〜1   c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜c〜3 < / p>

这是我最后的编辑代码,我被卡在这里了。

public class test {

    static Scanner x;

    public static void main(String [] args) {
        String filepath = "input.txt";
        String removeterm = "1";

        removeRecord(filepath,removeterm);

    }

    private static void removeRecord(String filepath, String removeterm) {

        String tempFile = "temp.txt";
        File oldfile = new File(filepath);
        File newfile = new File(tempFile);

        String ID1  = ""; String ID2  = ""; String ID3  = "";
        String ID4  = ""; String ID5  = ""; String ID6  = "";
        String ID7  = ""; String ID8  = ""; String ID9  = "";
        String ID10 = ""; String ID11 = ""; String ID12 = "";
        String ID13 = ""; String ID14 = ""; String ID15 = "";
        String ID16 = ""; String ID17 = ""; String ID18 = "";
        String ID19 = ""; String ID20 = ""; String ID21 = "";
        String ID22 = ""; String ID23 = ""; String ID24 = "";
        String ID25 = "";

        try
        {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            x = new Scanner(new File(filepath));
            x.useDelimiter("[~\n]");

            while(x.hasNext())
            {
                ID1  = x.next(); ID2  = x.next(); ID3  = x.next();
                ID4  = x.next(); ID5  = x.next(); ID6  = x.next();
                ID7  = x.next(); ID8  = x.next(); ID9  = x.next();
                ID10 = x.next(); ID11 = x.next(); ID12 = x.next();
                ID13 = x.next(); ID14 = x.next(); ID15 = x.next();
                ID16 = x.next(); ID17 = x.next(); ID18 = x.next();
                ID19 = x.next(); ID20 = x.next(); ID21 = x.next();
                ID22 = x.next(); ID23 = x.next(); ID24 = x.next();
                ID25 = x.next();

                if(!ID1.equals(removeterm)) { 
                    pw.println(ID1  + "~" + ID2 + "~" + ID3  + "~" + ID4  + "~" + ID5  + "~" + ID6  + "~" + ID7  + "~" + ID8  + "~" + ID9  + "~" + ID10 + "~" + ID11 + "~" + ID12 + "~" + ID13 + "~" + ID14 + "~" + ID15 + "~" + ID16 + "~" + ID17 + "~" + ID18 + "~" + ID19 + "~" + ID20 + "~" + ID21 + "~" + ID22 + "~" + ID23 + "~" + ID24 + "~" + ID25);
                    JOptionPane.showMessageDialog(null, "Done !");
                }
            }
            x.close();
            pw.flush();
            pw.close();
            oldfile.delete();
            File dump = new File(filepath);
            newfile.renameTo(dump);
        }
        catch (NoSuchElementException exception) {
            // Output expected NoSuchElementExceptions.
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Error !" + e);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

可能有帮助!
第1步:将文件读入列表(建议使用LinkedList),其元素为“ a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜a〜例如“ a〜a〜a〜a〜a〜a〜a〜a〜1”。 Refer

第2步:只需尝试删除list [i] .endWiths(“ yournumber”)的元素,然后重写文件或对列表进行一些处理即可。 Refer

答案 1 :(得分:-1)

使用Java8功能和NIO(自Java7开始),解决方案将非常简单:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        String filepath = "./input.txt";
        String removeterm = "1";

        removeRecord(filepath, removeterm);

    }

    private static void removeRecord(String filepath, String removeterm) {
        try {
            List<String> filtered = Files.readAllLines(Paths.get(filepath)).stream().filter(s -> !s.endsWith(removeterm)).collect(Collectors.toList());
            Files.write(Paths.get("./temp.txt"), filtered);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}