Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
eclipse buid path vs tomcat lib path 10 34
by zero exception 10 52
Running JavaFX on the Raspberry Pi 27 35
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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

790 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