Populating an arrayList after reading from a text file

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Populating an arrayList after reading from a text file

  1. #1
    Join Date
    Jul 2008

    Populating an arrayList after reading from a text file

    import java.io.*;
    import java.util.*;
    public class ReadTextFile {
      * Fetch the entire contents of a text file, and return it in a String.
      * This style of implementation does not throw Exceptions to the caller.
      * @param aFile is a file which already exists and can be read.
      static public ArrayList getContents(File aFile) {
    	  ArrayList arData = new ArrayList();
        //...checks on aFile are elided    
        try {
          //use buffering, reading one line at a time
          //FileReader always assumes default encoding is OK!
          BufferedReader input =  new BufferedReader(new FileReader(aFile));
          try {
            String line = null; //not declared within while loop
            * readLine is a bit quirky :
            * it returns the content of a line MINUS the newline.
            * it returns null only for the END of the stream.
            * it returns an empty String if two newlines appear in a row.
            while (( line = input.readLine()) != null){
    		  int index =line.indexOf(':');
    		  if(index != -1 )
          finally {
        catch (IOException ex){
    	return arData;
      /** Simple test harness.   */
      public static void main (String [] args) throws IOException {
    	  String fName="Demo.txt";
    	  if (args.length >0)
    	  File testFile = new File(fName);
    Last edited by Hack; 07-11-2008 at 01:21 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Apr 2007
    Sterling Heights, Michigan
    Welcome to DevX

    I see a thread title, which is a statement, and a bunch of code.

    What is your question?

Similar Threads

  1. Reading Text From a Text File
    By adey in forum VB Classic
    Replies: 1
    Last Post: 03-12-2007, 09:54 AM
  2. Importing text file using schema.ini
    By Kevin in forum VB Classic
    Replies: 3
    Last Post: 12-05-2005, 06:25 PM
  3. Replies: 0
    Last Post: 03-05-2001, 05:24 AM
  4. Problem: Reading text file with comma
    By Paolo in forum VB Classic
    Replies: 3
    Last Post: 01-08-2001, 08:59 AM
  5. Replies: 2
    Last Post: 05-31-2000, 10:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center