Home > Enterprise >  Deleting word inside text file
Deleting word inside text file

Time:05-17

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

    }

}

CodePudding user response:

Maybe you could store the words to be filtered in a 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");
        }
    }

}

Example Usage:

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

file.txt before:

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

file.txt after:

b d
b b b d e r

CodePudding user response:

Your code is filtering by looking at a line at a time and filtering by if it includes the word. If it does, the function returns false and the line isn't collected into the list. You need another layer of looping within the filter function that splits the line into individual words instead of looking at it as a whole. You need to use String.split for the line to split it into words then filter the array returned by Stirng.split to see if each word is equal to the target word.

  • Related