删除文本文件中的单词

Deleting word inside text file

我正在开发一个命令行应用程序,通过删除用户通过命令行指定的词来过滤文本文件。 当代码在行内找到过滤词时,它会删除整行而不仅仅是这个词。 有没有办法将这行拆分成单独的单词?


public class Main11 {

    public static void main(String[] args) {

        Scanner s=new Scanner (System.in);

        System.out.println("<Source file> <Words to filter>");

        String st1=s.nextLine();

        String [] split1=st1.split(" ");

        Path p = Paths.get(split1[0]);


        try {

            List<String> lines=Files.readAllLines(p, StandardCharsets.UTF_8);
            List<String> filter = lines;


            for(int i=0;i<lines.size();i++) {

                for(int j=0;j<split1.length;j++) {



                    final int x=j;

                    filter=filter.stream().filter(line-> !line.contains(split1[x])).collect(Collectors.toList());

                }
            }

            BufferedWriter writer = Files.newBufferedWriter(p);

            for(int c=0;c<filter.size();c++) {
                writer.write("\n" + filter.get(c));
            }

            writer.close();

            System.out.println("Done");

        } catch (IOException e) {

            System.out.println("file not found");
        }

        s.close();

    }

}

您的代码通过一次查看一行并根据是否包含该词进行过滤。如果是,则函数 returns false 并且该行不会收集到列表中。您需要在过滤器函数中使用另一层循环,将行拆分为单个单词,而不是将其作为一个整体来看待。您需要使用 String.split 将行拆分为单词,然后过滤 Stirng.split 返回的数组以查看每个单词是否等于目标单词。

也许您可以将要过滤的词存储在 Set:

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter: <source-file> <words-to-filter>");
        String input = scanner.nextLine();
        String[] inputSplit = input.split(" ");
        String sourceFile = inputSplit[0];
        Path p = Paths.get(sourceFile);
        Set<String> wordsToFilter = Set.of(Arrays.copyOfRange(inputSplit, 1, inputSplit.length));
        try {
            List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);
            List<String> filteredLines = new ArrayList<>(lines.size());
            for (String line : lines) {
                filteredLines.add(Arrays.stream(line.split(" "))
                    .filter(word -> !wordsToFilter.contains(word))
                    .collect(Collectors.joining(" ")));
            }
            BufferedWriter writer = Files.newBufferedWriter(p);
            for (String filteredLine : filteredLines) {
                if (!filteredLine.isEmpty()) {
                    writer.write(filteredLine + "\n");
                }
            }
            writer.close();
            System.out.println("Done filtering!");
        } catch (IOException e) {
            System.err.println("Error: file not found");
        }
    }

}

用法示例:

Enter: <source-file> <words-to-filter>
/Users/shash/Desktop/file.txt a c
Done filtering!

file.txt之前:

a a a
a a a b a c d a
b b b d e r

file.txt 之后:

b d
b b b d e r