I amdeveloping a command line application to filter a text file by deleting the words specified by the user via the command line. when the code finds the filter word inside the line its deleting the whole line not just the word. Is there a way to split the line into separate words?
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();
}
}