Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 90
micro services vs rest web services 16 103
mockito example issue 8 72
pagenation logic how it is working in my code 1 27
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

786 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