View Javadoc

    1   package baseCode.io.reader;
    2
    3   import java.io.BufferedReader;
    4   import java.io.File;
    5   import java.io.FileInputStream;
    6   import java.io.IOException;
    7   import java.io.InputStream;
    8   import java.io.InputStreamReader;
    9   import java.util.HashMap;
    10  import java.util.HashSet;
    11  import java.util.Map;
    12  import java.util.Set;
    13  import java.util.StringTokenizer;
    14
    15  /***
    16   * Reads a tab-delimited file with lines of the format Key Value. If there are multiple values, then a Set is created
    17   * for each key containing its values.
    18   * </p>
    19   * <p>
    20   * Copyright (c) 2004
    21   * </p>
    22   * <p>
    23   * Institution: Columbia University
    24   * </p>
    25   *
    26   * @author Paul Pavlidis
    27   * @version $Id: MapReader.java,v 1.2 2004/07/27 03:18:58 pavlidis Exp $
    28   */
    29  public class MapReader {
    30
    31     /***
    32      * @param filename String
    33      * @throws IOException
    34      * @return Map
    35      */
    36     public Map read( String filename ) throws IOException {
    37        return this.read( filename, false );
    38     }
    39
    40     /***
    41      * @param stream InputStream
    42      * @return @throws IOException
    43      * @throws IOException
    44      */
    45     public Map read( InputStream stream ) throws IOException {
    46        return this.read( stream, false );
    47     }
    48
    49     /***
    50      * @param filename name of the tab-delimited file
    51      * @param hasHeader boolean
    52      * @return Map from the file.
    53      * @throws IOException
    54      */
    55     public Map read( String filename, boolean hasHeader ) throws IOException {
    56        File infile = new File( filename );
    57        if ( !infile.exists() || !infile.canRead() ) {
    58           throw new IllegalArgumentException( "Could not read from " + filename );
    59        }
    60        FileInputStream stream = new FileInputStream( infile );
    61        return read( stream, hasHeader );
    62
    63     }
    64
    65     /***
    66      * @param stream InputStream
    67      * @param hasHeader boolean
    68      * @return @throws IOException
    69      * @throws IOException
    70      */
    71     public Map read( InputStream stream, boolean hasHeader ) throws IOException {
    72        Map result = new HashMap();
    73
    74        BufferedReader dis = new BufferedReader( new InputStreamReader( stream ) );
    75        if ( hasHeader ) {
    76           dis.readLine();
    77        }
    78
    79        String row;
    80        while ( ( row = dis.readLine() ) != null ) {
    81           StringTokenizer st = new StringTokenizer( row, "\t" );
    82           String key = st.nextToken();
    83
    84           String value = st.nextToken();
    85
    86           if ( st.hasMoreTokens() ) {
    87              Set innerList = new HashSet();
    88              innerList.add( value );
    89              while ( st.hasMoreTokens() ) {
    90                 value = st.nextToken();
    91              }
    92              innerList.add( value );
    93              result.put( key, innerList );
    94           } else {
    95              result.put( key, value );
    96           }
    97        }
    98        dis.close();
    99
    100       return result;
    101    }
    102 } // end of class