Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reading lines of text file

Posted on 2004-11-30
6
Medium Priority
?
251 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:VbMonk
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12710826
You'll have to cache at least some of the lines in memory  if that's what you want to do
0
 

Author Comment

by:VbMonk
ID: 12710890
How can that be done?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 12710973
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
 
LVL 92

Expert Comment

by:objects
ID: 12711154
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question