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 LinkedHashMap .

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 LinkedHashMap<String, String> readFileToHashMap(String filePath, String separator,
        boolean valueFirst) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w w .j  av a  2 s.c  o m*/
 * JGrass - Free Open Source Java GIS http://www.jgrass.org 
 * (C) HydroloGIS - www.hydrologis.com 
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Library General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 * 
 * This library 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 Library General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Library General Public License
 * along with this library; if not, write to the Free Foundation, Inc., 59
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.*;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class Main {
    /**
     * Method to read a properties file into a {@link LinkedHashMap}. <p/> <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 LinkedHashMap<String, String> readFileToHashMap(String filePath, String separator,
            boolean valueFirst) throws IOException {
        if (separator == null) {
            separator = "=";
        }
        List<String> lines = readFileToLinesList(filePath);
        LinkedHashMap<String, String> propertiesMap = new LinkedHashMap<>();
        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 filePath 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 {
        List<String> lines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                lines.add(line);
            }
            return lines;
        }
    }
}

Related

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