Java BufferedReader Read readFileToHashMap(String filePath, String separator, boolean valueFirst)

Here you can find the source of readFileToHashMap(String filePath, String separator, boolean valueFirst)

Description

Method to read a properties file into a HashMap .

License

Open Source License

Parameter

Parameter Description
filePath the path to the file to read.
separator the separator or <code>null</code>. Defaults to '='.
valueFirst if <code>true</code>, the second part of the string is used as key.

Exception

Parameter Description
IOException an exception

Return

the read map.

Declaration

public static HashMap<String, String> readFileToHashMap(String filePath, String separator, boolean valueFirst)
        throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w ww.  j  a v  a 2s . c o m*/
 * Stage - Spatial Toolbox And Geoscript Environment 
 * (C) HydroloGIS - www.hydrologis.com 
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * (http://www.eclipse.org/legal/epl-v10.html).
 */

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Main {
    /**
     * Method to read a properties file into a {@link HashMap}.
     * 
     * <p>Empty lines are ignored, as well as lines that do not contain the
     * separator.</p>
     * 
     * @param filePath the path to the file to read.
     * @param separator the separator or <code>null</code>. Defaults to '='.
     * @param valueFirst if <code>true</code>, the second part of the string is used as key. 
     * @return the read map.
     * @throws IOException 
     */
    public static HashMap<String, String> readFileToHashMap(String filePath, String separator, boolean valueFirst)
            throws IOException {
        if (separator == null) {
            separator = "=";
        }
        List<String> lines = readFileToLinesList(filePath);
        HashMap<String, String> propertiesMap = new HashMap<String, String>();
        for (String line : lines) {
            line = line.trim();
            if (line.length() == 0) {
                continue;
            }
            if (!line.contains(separator)) {
                continue;
            }
            String[] lineSplit = line.split(separator);
            if (!valueFirst) {
                String key = lineSplit[0].trim();
                String value = "";
                if (lineSplit.length > 1) {
                    value = lineSplit[1].trim();
                }
                propertiesMap.put(key, value);
            } else {
                if (lineSplit.length > 1) {
                    String key = lineSplit[0].trim();
                    String value = lineSplit[1].trim();
                    propertiesMap.put(value, key);
                }
            }
        }
        return propertiesMap;
    }

    /**
     * Read text from a file to a list of lines.
     * 
     * @param outFile the path to the file to read.
     * @return the list of lines.
     * @throws IOException 
     */
    public static List<String> readFileToLinesList(String filePath) throws IOException {
        return readFileToLinesList(new File(filePath));
    }

    /**
     * Read text from a file to a list of lines.
     * 
     * @param file the file to read.
     * @return the list of lines.
     * @throws IOException 
     */
    public static List<String> readFileToLinesList(File file) throws IOException {
        BufferedReader br = null;
        List<String> lines = new ArrayList<String>();
        try {
            br = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            return lines;
        } finally {
            if (br != null)
                br.close();
        }
    }
}

Related

  1. readFileContext(File fileToRead)
  2. readFileEncoding(String file)
  3. readFileGdeHash(File gdeFile, ArrayList name, ArrayList seq)
  4. readFilePathToString(String filePath)
  5. readFileToHashMap(String filePath, String separator, boolean valueFirst)
  6. readFileToList(String fileName, List list)
  7. readFileToList(String path, String split)
  8. readFileToListOfStrings(File file)
  9. readFileValue(File file)