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 7419909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:09:04+00:00 2026-05-29T08:09:04+00:00

I need to process an input file, and copy its content (line by ilne)

  • 0

I need to process an input file, and copy its content (line by ilne) to an output file. However, there are some unimportant data (stray) inside this input file that I need to skip.
The main problem that I am trying to solve is actually more complicated than this, but I am just gonna simplify the problem:

So, I have an input file containing hundreds of thousands of lines.
If the following sequence of 3-lines occur inside the input file:
A
B
C

then I need to skip these 3 lines and proceed with the next line in the input file. I can only skip these 3 lines if these 3 lines occur as a sequence of consecutive lines.

For example:
Input File:

A
A
B
C
B
P
A
B
C
A
B
A
A
B
C
A

Output file:
A
B
P
A
B
A
A

Clarification:
A
A (skipped)
B (skipped)
C (skipped)
B
P
A (skipped)
B (skipped)
C (skipped)
A
B
A
A (skipped)
B (skipped)
C (skipped)
A

Notice that I can skip the sequence of lines (A, B, C) only if they occur sequentially. All the other lines which are not skipped have to be copied to the output file.
If I use BufferedReader.nextLine(), I cannot backtrack to the previous lines if the next line doesn’t match the input pattern. For example, if I already encounter an A, and the next line is another A (not B), I then have to copy the first A to the output file, and start the filtering again from the second A which I have not processed, and check the next following line, and so on.

One way that I can think of is to firstly save the contents of the input text file, so I can easily backtrack when traversing the input file contents if it doesn’t match the pattern I am looking for. However this is not a memory-wise solution. Is there any clever algorithm to solve this, preferably in one time traversal, i.e. O (N) complexity? Or if this is not possible, what would be the most optimal solution which is still memory-wise? Some example C / Java codes will be really helpful.

  • 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-05-29T08:09:04+00:00Added an answer on May 29, 2026 at 8:09 am

    You could do this with a 3-element array.

    Whenever you encounter an A, check if the first element of the array is empty — if not, flush the array to the output file — then store the new A to the first element of the array.

    Whenever you encounter a B, check if the second element of the array is empty but the first element is full — if not, flush the array to the output file along with the new B. Otherwise (that is, if the first element is full but the 2nd is empty) you’ll store the new B as the 2nd element of the array.

    For C, repeat the logic for B, incremented by one: Whenever you encounter a C, check if the third element of the array is empty but the 2nd element is full — if not, flush the array to the output file along with the new C. Otherwise (that is, if the 2nd element is full but the 3rd is empty) you’ll store the new C as the 3rd element of the array.

    When you encounter neither A nor B nor C, flush any existing array elements to the output file, then write the new line directly to the output file.

    The main trick here is that you’re defining explicit rules for filling each slot of the buffer array, and using this to avoid re-checking any line matches, while flushing the buffer to the output and resetting the sequence whenever you break pattern.

    Granted, you admit that your actual ruleset is somewhat more complicated, but the same type of approach should work.

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

Sidebar

Related Questions

I need to be able to process an input file where there are 3
This is the new input file format. I need to automate the process of
I need some help. I'm trying to upload files using <input type=file> . Here
I need to process a large file, around 400K lines and 200 M. But
I need to process a HTML content and replace the IMG SRC value with
I need to process the incoming predefined ASN format data(coming from verity of clients
I have a quite big XML output from an application. I need to process
Is there a way to easily create Java Input/Output streams for unnamed pipes created
I have an Excel file that I need to process three times in integration
I want to read and process sets of input from a file and then

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.