DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2008
    Posts
    1

    Populating an arrayList after reading from a text file

    Code:
    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 )
    		  {
    			arData.add(line.substring(0,index));//name
    			arData.add(line.substring(index+1,line.length()));//value
    		  }
            }
          }
          finally {
            input.close();
          }
        }
        catch (IOException ex){
          ex.printStackTrace();
        }
    	return arData;
      }
      /** Simple test harness.   */
      public static void main (String [] args) throws IOException {
    	  String fName="Demo.txt";
    	  if (args.length >0)
    	  {
    		  fName=args[0];
    	  }
    	  File testFile = new File(fName);
    	  System.out.println(getContents(testFile));
      }
    }
    Last edited by Hack; 07-11-2008 at 01:21 PM. Reason: Added Code Tags

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Welcome to DevX

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

    What is your question?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

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

Bookmarks

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links