Java BufferedReader Read loadTwoColumnResource(InputStream resource)

Here you can find the source of loadTwoColumnResource(InputStream resource)

Description

Function reads two column file and stores the values into a HashMap object

License

Open Source License

Parameter

Parameter Description
resource : InputStream containing the two column resource

Exception

Parameter Description
IOException if the given resource has reading problems.

Return

HashMap contains the elements and their respective attribute values

Declaration

public static HashMap<String, String> loadTwoColumnResource(InputStream resource) throws IOException 

Method Source Code


//package com.java2s;
/*/* ww  w .  ja  v  a 2  s  . c  om*/
 * Copyright 2014 Elhuyar Fundazioa
    
This file is part of EliXa.
    
EliXa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
EliXa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with EliXa.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.HashMap;

public class Main {
    /**
     * Function reads two column file and stores the values into a HashMap<String,String> object 
     * 
     * @param resource : InputStream containing the two column resource
     * @return HashMap<String,String> contains the elements and their respective attribute values 
     * 
     * @throws IOException if the given resource has reading problems.
     */
    public static HashMap<String, String> loadTwoColumnResource(InputStream resource) throws IOException {
        HashMap<String, String> result = new HashMap<String, String>();

        BufferedReader breader = new BufferedReader(new InputStreamReader(resource));
        String line;
        while ((line = breader.readLine()) != null) {
            if (line.startsWith("#") || line.matches("^\\s*$")) {
                continue;
            }
            String[] fields = line.split("\t");
            try {
                result.put(fields[0], fields[1]);
            } catch (IndexOutOfBoundsException ioobe) {
                System.err.println("FileUtilsElh::loadTwoColumnResource - " + line);
            }
        }
        breader.close();
        return result;
    }
}

Related

  1. loadTestFixture(String path)
  2. loadTestUserEventRelation(String eventFilePath, HashMap> TestUser2EventIndexSetMap)
  3. loadToStringListFromFile(String fullFileName)
  4. loadTrustDistrustSets(String trust_data_set)
  5. loadTrustSet(String trustSet)
  6. loadTxnDescriptions(String filename)
  7. loadTXTList(String listFile, int size)
  8. loadUnicodeStringFromFile(final File file)
  9. loadVocab(String vocabFilePath, double factor)