Solved

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

Posted on 2006-11-02
4
199 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

14 Experts available now in Live!

Get 1:1 Help Now