Reading lines of text file

I want to do someting odd with text files. I have a text file whose contents are lines like the ones below:

line 1
line 2
line 3
line 4
line 5

I want to read these lines from a text file, add lines 2, 3 and line 4 if line one contains a certain number like 1000 or if line2 has some number say 202, it should add lines 1 to line 16. The list in the text file is very long that putting it in an array would take so much memory.
How can i possibly do this?
VbMonkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You'll have to cache at least some of the lines in memory  if that's what you want to do
0
VbMonkAuthor Commented:
How can that be done?
0
CEHJCommented:
This would hold the whole file in memory. You'd have to cut this down to how many you can/need to hold:


import java.io.*;
import java.util.ArrayList;

/**
 *      A utility class the allows a text file to be
 *      treated as a collection of Strings
 *
 * @author             CEHJ
 * @created            29 February 2004
 */
public class StringList extends ArrayList {

      /**
       *Constructor for the StringList object
       */
      public StringList() {
            super();
      }

      public void read(InputStream in) {
            read(new InputStreamReader(in));
      }


      public void read(Reader r) {
            String line = null;
            BufferedReader in = null;
            try {
                  in = new BufferedReader(r);
                  while ((line = in.readLine()) != null) {
                        add(line);
                  }
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            finally {
                  try {
                        in.close();
                  }
                  catch (IOException e) {
                        e.printStackTrace();
                  }
            }

      }

      /**
       *      Constructor for the StringList object
       *
       * @param      fileName      The file to open
       */
      public void read(String fileName) {
            try {
                  read(new FileReader(fileName));
            }
            catch(IOException e) {
                  e.printStackTrace();
            }
      }

      /**
       *      Save the String to named file
       *
       * @param      fileName      The name of the file to save to
       */
      public void save(String fileName) {
            PrintWriter out = null;
            try {
                  out = new PrintWriter(new FileOutputStream(fileName), true);
                  for (int i = 0; i < size(); i++) {
                        out.println((String) get(i));
                  }
            }
            catch (IOException e) {
                  e.printStackTrace();
            }
            finally {
                  if (out != null) {
                        out.close();
                  }
            }
      }

}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
you need to either keep as many lines in memory as you need.
Or perform more than one pass over the file, eg. read it twice.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.