Java URL Load readFileIntoMap(URL url)

Here you can find the source of readFileIntoMap(URL url)

Description

Reads a text file with KVP content into a map.

License

Open Source License

Parameter

Parameter Description
url url of the text file

Exception

Parameter Description
IOException if the the file cannot be loaded

Return

map with the contents of the file, keys are uppercased

Declaration

public static Map<String, String> readFileIntoMap(URL url)
        throws IOException 

Method Source Code

//package com.java2s;
/*----------------------------------------------------------------------------
 This file is part of deegree, http://deegree.org/
 Copyright (C) 2001-2009 by://from   ww w . java 2 s  .c om
 Department of Geography, University of Bonn
 and
 lat/lon GmbH

 This library is free software; you can redistribute it and/or modify it under
 the terms of the GNU Lesser General Public License as published by the Free
 Software Foundation; either version 2.1 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 Lesser General Public License for more
 details.
 You should have received a copy of the GNU Lesser General Public License
 along with this library; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

 Contact information:

 lat/lon GmbH
 Aennchenstr. 19, 53177 Bonn
 Germany
 http://lat-lon.de/

 Department of Geography, University of Bonn
 Prof. Dr. Klaus Greve
 Postfach 1147, 53001 Bonn
 Germany
 http://www.geographie.uni-bonn.de/deegree/

 e-mail: info@deegree.org
 ----------------------------------------------------------------------------*/

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.URL;
import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Reads a text file with KVP content into a map.
     * <p>
     * Example contents:
     * 
     * <pre>
     * SERVICE=WFS
     * VERSION=1.1.0
     * REQUEST=DescribeFeatureType
     * TYPENAME=TreesA_1M
     * </pre>
     * 
     * What this method does:
     * <ul>
     * <li>Every line is split around the '=' character, the first part is used as the key, the second part as the value
     * (if the line doesn't contain a '=', it is ignored).</li>
     * <li>Keys are uppercased.</li>
     * <li>Values are URL decoded.</li>
     * </ul>
     * </p>
     * 
     * @param url
     *            url of the text file
     * @return map with the contents of the file, keys are uppercased
     * @throws IOException
     *             if the the file cannot be loaded
     */
    public static Map<String, String> readFileIntoMap(URL url)
            throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String line = null;
        Map<String, String> params = new HashMap<String, String>();
        while ((line = reader.readLine()) != null) {
            if (line.contains("=")) {
                String[] parts = line.split("=");
                if (parts[0].equalsIgnoreCase("FILTER")) {
                    params.put("FILTER",
                            URLDecoder.decode(line.substring(7), "UTF-8"));
                } else if (parts[0].equalsIgnoreCase("NAMESPACE")) {
                    params.put("NAMESPACE", line.substring(10));
                } else if (parts.length == 2) {
                    params.put(parts[0].toUpperCase(),
                            URLDecoder.decode(parts[1], "UTF-8"));
                } else {
                    throw new IllegalArgumentException();
                }
            }
        }
        return params;
    }
}

Related

  1. readFile(URL file)
  2. readFile(URL url)
  3. readFile(URL url)
  4. readFileContent(URL file)
  5. readFileFromURL(URL url)
  6. readFileIntoString(URL input)
  7. readFileIntoString(URL url)
  8. readFromFile(URL fileName)
  9. readFromFile(URL source)