Results 1 to 6 of 6
  1. #1
    Boaz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Iterate Text File

    hi all,

    i have a small question,

    How can i scan a text file using iterator ?

    i have a function that get as a parameter an iterator
    and i want that iterator will be scanning the text file line by line..


    the reson i want to use iterator is
    i don't want to load the data into memory (using array or list)
    because i have a very big data to read..

    code example will be very helpfull (:

    Thanks a head
    Boaz

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Boaz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default yes , it's possible

    i implement Iterator interface
    and now i can do it..

    thanks any way

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    Boaz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Scan text file with out load to memory

    you can use that implementation:

    Java Code:
    import java.util.Iterator;
    import java.io.*;
    
    /**
     * This class allows line-by-line iteration through a text file.
     * The iterator's remove() method throws UnsupportedOperatorException.
     * The iterator wraps and rethrows IOExceptions as IllegalArgumentExceptions.
     */
    public class TextFile implements Iterable<String>
    {
    	// Used by the TextFileIterator class below
    	final String filename;
    
    
    	public TextFile(String filename)
    	{
    		this.filename = filename;
    	}
    
    	// This is the one method of the Iterable interface
    	public Iterator<String> iterator()
    	{
    		return new TextFileIterator();
    	}
    
    
    	// This non-static member class is the iterator implementation
    	class TextFileIterator implements Iterator<String>
    	{
    
    		BufferedReader in;// The stream we're reading from
    		String nextline;// Return value of next call to next()
    
    		public TextFileIterator()
    		{
    			// Open the file and read and remember the first line.
    			// We peek ahead like this for the benefit of hasNext().
    			try
    			{
    				in = new BufferedReader(new FileReader(filename));
    				nextline = in.readLine();
    
    			}
    			catch(IOException e)
    			{
    				throw new IllegalArgumentException(e);
    			}
    		}
    
    		// If the next line is non-null, then we have a next line
    		public boolean hasNext()
    		{
    			return nextline != null;
    		}
    
    		// Return the next line, but first read the line that follows it.
    		public String next()
    		{
    			try
    			{
    				String result = nextline;
    
    				// If we haven't reached EOF yet
    				if (nextline != null)
    				{
    					nextline = in.readLine(); // Read another line
    					if (nextline == null)
    					in.close();             // And close on EOF
    				}
    
    				// Return the line we read last time through.
    				return result;
    			}
    			catch(IOException e)
    			{
    				throw new IllegalArgumentException(e);
    			}
    		}
    
    		// The file is read-only; we don't allow lines to be removed.
    		public void remove()
    		{
    			throw new UnsupportedOperationException();
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		String filename = "TextFile.java";
    		if (args.length > 0)
    			filename = args[0];
    
    		for(String line : new TextFile(filename))
    			System.out.println(line);
    	}
    }

    Good luck (:
    Last edited by Eranga; 03-17-2009 at 09:54 AM. Reason: Added code tags

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. find and replace text from a text file
    By gezzel in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM
  3. Using Enumeration to iterate through Hashtable
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 09:44 AM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. iterate HashMap with logic
    By Heather in forum Web Frameworks
    Replies: 2
    Last Post: 07-03-2007, 10:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •