Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7831523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:46:13+00:00 2026-06-02T11:46:13+00:00

I have to remove common words like (is,are,am,was etc) from a text file. what

  • 0

I have to remove common words like (is,are,am,was etc) from a text file. what is the efficient way of doing it in java ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T11:46:16+00:00Added an answer on June 2, 2026 at 11:46 am

    You will have to read the file in, skipping the words you want to remove, and then write the file back out again.

    Because of this, you may prefer to just skip the words you want to ignore each time you read it – depends on your use case.

    To actually remove the words on a line-by-line basis (which may not be the way you want to do it anyway), you could do this (using google guava):

        // the words you want to remove from the file:
        //
        Set<String> wordsToRemove = ImmutableSet.of("a", "for");
    
        // this code will run in a loop reading one line after another from the file
        //
        String line = "Some words read from a file for example";
        StringBuffer outputLine = new StringBuffer();
        for (String word : Splitter.on(Pattern.compile("\\s+")).trimResults().omitEmptyStrings().split(line)) {
            if (!wordsToRemove.contains(word)) {
                if (outputLine.length() > 0) {
                    outputLine.append(' ');
                }
                outputLine.append(word);
            }
        }
    
        // here I'm just printing, but this line could now be written to the output file.
        //
        System.out.println(outputLine.toString());
    

    Running this code will output:

    Some words read from file example
    

    I.e, “a” and “for” are omitted.

    Notice that this makes for simple code, but, it will change the whitespace formatting in your file. If you had a line with doubled up spaces, tabs etc, then this all gets changed to a single space in this code. This is just an example of how you might do it, depending on your requirements, there will probably be better ways.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to remove duplicate strings from extremely big text file (100 Gb+) Since
I have a text file that maintains a list of words. What I am
I have this code here, it is supposed to remove the common letters from
I have to remove a branch from svn history for good. Normally I would
say I have a string like so Testing How could I have it remove
I am having on xml from which i have to remove the one book
With python inotifyx, do I have to remove watch and close opened system file
I have following code to remove group from collection. Technically, there should be no
I have a problem where I need to remove all code and triggers from
I have a table of common words that are used in sentences (i.e. A,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.