Tuesday, September 24, 2013

Reading Flat files seperated by | symbol

Often I have seen that there are files having lines seperated by a de-limiter |.
I thought of making a class that could easily iterate over each of the line one by one..

This class PipedString represents a string which has elements seperated by a | symbol.
This is an iterable class

Create a class PipedString as follows

package flatfilereader;

import java.util.Iterator;

public class PipedString implements Iterable<String> {
    private String line;
    private int index = 0;
    @Override
    public String toString()
    {
        return line;
    }
    public PipedString(String x) {
        this.line = x;
    }
    @Override
    public Iterator<String> iterator() {
        return new Iterator<String>()
        {
            private String _currentElement;
            String[] symbols=null;
            @Override
            public boolean hasNext() {
                try {
                     if(symbols==null) 
                     {
                         symbols = line.split("\\|");
                     }
                    _currentElement = symbols[index];
                } catch (Exception ex) {
                    line = null;
                }

                return (index < symbols.length);
            }

            @Override
            public String next() {
                index++;
                return _currentElement;
            }

            @Override
            public void remove() {
            }
        };
    }
}

Now create a Flat File Reader using this PipedString

package flatfilereader;
import java.util.*;
import java.io.*;
  
public class FlatFileReader implements Iterable<PipedString>
{
    private BufferedReader _reader;
  
    public FlatFileReader(String filePath) throws Exception
    {
        _reader = new BufferedReader(new FileReader(filePath));
    }
  
    public void Close()
    {
        try
        {
            _reader.close();
        }
        catch (Exception ex) {}
    }
  
    public Iterator<PipedString> iterator()
    {
        return new PipedStringIterator();
    }
    private class PipedStringIterator implements Iterator<PipedString>
    {
        private PipedString _currentLine;
  
        public boolean hasNext()
        {
            try
            {
                String cl = _reader.readLine();
                if(cl==null) return false;
                _currentLine = new PipedString(cl);
            }
            catch (Exception ex)
            {
                _currentLine = null;
            }
  
            return _currentLine != null;
        }
  
        @Override
        public PipedString next()
        {
            return _currentLine;
        }
  
        public void remove()
        {
        }
    }
}

How to use this FlatFileReader class

    public static void main(String[] s) throws Exception
    {
        FlatFileReader f= new FlatFileReader("D:/test/x.txt");
        for(PipedString pps : f)
        {
            for(String eachElement : pps)
            {
                System.out.println(eachElement);
            }
        }
        f.Close();
    }


No comments:

Post a Comment