1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by r3drock3t88, Apr 10, 2008.

Thread Status:
Not open for further replies.
Advertisement
  1. r3drock3t88

    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
     
  2. Chicon

    Chicon

    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]
     
  3. r3drock3t88

    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!
     
  4. Chicon

    Chicon

    Joined:
    Jul 29, 2004
    Messages:
    6,650
    (y) You're welcome ! :)
     
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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/702468

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice