Solved

Opening Text File, Read Each Line in it and Output With Line Number Before Each Line

Posted on 2006-11-02
4
200 Views
Last Modified: 2010-03-31
Hello Experts,

I have a file that I cannot get to work properly after several attempts... I am trying to read every line from a text file called inventory.txt  which contains:

bear, 6 94962 11263 4
ball, 4 89012 48129 3
train, 6 94551 11003 3
doll, 6 94551 11263 4
train, 6 94551 11003 3
car, 4 32551 48003 3
ball, 4 89012 48129 3
doll, 6 94551 11263 4

then output each line to the screen with a line number before them... Here is my code so far that does not work properly, I am sure there is a more efficient way of getting it to work...

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

public class ReadTextFile
{
    public static void main(String[] args)
    {
        ReadTextFile inventory = new ReadTextFile();
        ArrayList newFile = inventory.ReadFile("inventory.txt");
        int fsize = 0;
        int ln = 0;
        while (fsize < newFile.size())
       {
                ln = fsize + 1;
                System.out.println("line: " + ln + "\t" + newFile.get(fsize));
                fsize++;
        }
    }

    public ArrayList ReadFile(String fileName)
    {

        String thisLine;
        ArrayList file = new ArrayList();
        try
        {
            BufferedReader buffer = new BufferedReader(new FileReader(fileName));

            thisLine = buffer.readLine();
            while (thisLine != null)
            {
                  
                  file.add(thisLine);
                  thisLine = buffer.readLine();
            }
            buffer.close();
        }
            
      catch (IOException e)
            {
                  
            }
      return file;
    }
}











0
Comment
Question by:tristatefab
  • 2
4 Comments
 

Expert Comment

by:David_Ryan
ID: 17863405
Assuming that inventory.txt is in the C:\Temp\ directory, this line:
ArrayList newFile = inventory.ReadFile("inventory.txt");

Should be like this:

ArrayList newFile = inventory.ReadFile("C:" + File.separator + "Temp" + File.separator + "inventory.txt");

With that change, the program works fine for me.

0
 

Author Comment

by:tristatefab
ID: 17863434
How could I modify the program to pass the file name as an argument?
0
 

Author Comment

by:tristatefab
ID: 17863459
How could I modify the program to pass the file name as an argument? I need to use FileReader and BufferedReader to open and read it somehow.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 17863493
outputting it as you read it instead of storing the file in a list would be more efficient.

to pass the filename as an arg use:

        ArrayList newFile = inventory.ReadFile(args[0]);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

948 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now