Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

reading lines from text file (java)

Original Message
Name: nunyabiznaz
Date: February 28, 2005 at 00:20:00 Pacific
Subject: reading lines from text file (java)
OS: win xp
CPU/Ram: p2
Comment:
What would be the best way to read lines from a text file in java?
The user gives me a text file which is already in a given format (i know how many lines the file has and which lines are Strings or integers).

example of a text file would be:

1
2
green
blue

i want to take the first line and say if its 1, move onto this method and if its 2 move onto this method, then both of those methods (whichever is called tho) will look at the second line and do the same. Then the last two lines are taken as Strings and = some data members say favoriteColor1 and favoriteColor2 and then from there i can use those data members in the rest of the program. thanks.


Report Offensive Message For Removal


Response Number 1
Name: Dr. Nick
Date: February 28, 2005 at 10:21:47 Pacific
Subject: reading lines from text file (java)
Reply: (edit)
There's a few ways you can do this (maybe something new in Java 1.5), but to read a general file in I usually like to read line by line and store it in an ArrayList.

This example I have should help:

=====================================================================

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

public class FileIO
{
    public static void main(String[] args)
    {
        ArrayList bob = loadFile(args[0]);
        for (int i=0; i<bob.size(); i++)
            System.out.println(i+1 + ":\t" + bob.get(i));
    }

    public static ArrayList loadFile(String fileName)
    {
        if ((fileName == null) || (fileName == ""))
            throw new IllegalArgumentException();
        
        String line;
        ArrayList file = new ArrayList();

        try
        {    
            BufferedReader in = new BufferedReader(new FileReader(fileName));

            if (!in.ready())
                throw new IOException();

            while ((line = in.readLine()) != null)
                file.add(line);

            in.close();
        }
        catch (IOException e)
        {
            System.out.println(e);
            return null;
        }

        return file;
    }
}



Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: reading lines from text file (java)

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12

help mandriva install last straw!

Icon Scaling in Explorer Bar


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC