【问题标题】:How to remove selected line when argument is found at the end of data?在数据末尾找到参数时如何删除选定的行?
【发布时间】:2019-06-15 16:46:14
【问题描述】:

我的文本文件如下:

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~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~3

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

我已经使用 Delimeter 找到了该数字。 但我不知道如何删除整行。

例如我搜索 No.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 ~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~3

这是我最后一次编辑代码,我卡在这里..

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

【问题讨论】:

    标签: java eclipse bufferedreader printwriter bufferedwriter


    【解决方案1】:

    可能会有所帮助!
    第 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~1" 例如。 Refer

    第 2 步:尝试删除 list[i].endWiths("yournumber") 的元素,然后重写文件或对列表执行一些操作。 Refer

    【讨论】:

    • 非常感谢Thanh提供的信息,我会从你的附件中学习更多。 ^_^
    【解决方案2】:

    使用 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();
            }
        }
    }
    

    【讨论】:

    • 非常感谢NAYA,你的代码是我一直在寻找和研究的。
    • 很高兴听到,比你。可能可以将其标记为答案。
    猜你喜欢
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2012-12-27
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多