Solved: Reading text file line by line in JAVA - Help PLZ

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

r3drock3t88

Thread Starter
Joined
Jan 12, 2007
Messages
272
Hey all,

Take for example the following text file...

a
aa
aaabaaa
madamiamadam

...

I have this code here to read the file,

Code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.FileInputStream;
public class FileCode
    {

    public static void main(String[] args) throws IOException
        {
        
        int next;
        char c;
        char[] message = new char[10000];
        String str;
        int count = 0;
        Stack stack = new Stack();
        
        
        String fileName = "c:\\prog4DS.dat";
        FileReader reader = new FileReader(fileName);
        FileWriter writer = new FileWriter("FileStuff.dat");
        
        next = reader.read();
        
        while(next != -1)
            {
                c = (char) next;
                message[count] = c;
                next = reader.read();
                count++;
            }
        
        reader.close();
        
        for(int i = 0; i < count; i++)
        {
            System.out.print(message[i]);
            writer.write(message[i]); 
        }

        writer.close();

        }

    }
Basically what I want is to read each line of the text file individually and store the text in its own location of an array. So for example, for the first line in the text file it is simply just an "a". I would like the program to recognize that "a", and assign it to the array.

Could anyone please assist me on how I'd go about doing this? Thank you all very much! It's much appreciated
 
Joined
Jul 29, 2004
Messages
6,650
Hi r3drock3t88,

The best way to read a file line by line is to use an instance of the BufferedReader class.
Example :
Code:
[SIZE=2]
   List<String> list = new ArrayList<String>();
   String line;
   ...
   ...
   String fileName = "c:\\prog4DS.dat";
   FileReader reader = new FileReader(fileName);
   BufferedReader bufrdr = new BufferedReader(reader);
   line = bufrdr.readLine();
   while (line != null) {
      list.add(line);
      line = bufrdr.readLine();
   }
   bufrdr.close();
   reader.close();
   ...
   ...
   String[] array = new String[list.size()];
   list.toArray(array);
[/SIZE]
 

r3drock3t88

Thread Starter
Joined
Jan 12, 2007
Messages
272
Thank you Chicon,

I used the bufferreader and managed to get my project finished :). Thanks again!
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top