Java File System loadSystemResourceKeyValueCsvFileToMap(String resourcePath)

Here you can find the source of loadSystemResourceKeyValueCsvFileToMap(String resourcePath)

Description

Loads the given system resource and assumes it is a csv file with a key and value column.

License

Open Source License

Parameter

Parameter Description
resourcePath Path to resource file that is to be loaded into a map.

Exception

Parameter Description
IOException an exception
URISyntaxException an exception

Return

Map loaded with key/value pairs from the resource file.

Declaration

public static Map<String, Double> loadSystemResourceKeyValueCsvFileToMap(String resourcePath)
        throws IOException, URISyntaxException 

Method Source Code

//package com.java2s;
/*//w ww  .ja v  a 2s.c om
 *     Copyright (C) 2015  higherfrequencytrading.com
 *
 *     This program 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 3 of the License.
 *
 *     This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Main {
    /**
     * Loads the given system resource and assumes it is a csv file with a key and value column. These key/value pairs
     * are loaded into a map.
     *
     * @param resourcePath Path to resource file that is to be loaded into a map.
     * @return Map loaded with key/value pairs from the resource file.
     * @throws IOException
     * @throws URISyntaxException
     */
    public static Map<String, Double> loadSystemResourceKeyValueCsvFileToMap(String resourcePath)
            throws IOException, URISyntaxException {
        URL testFileUrl = ClassLoader.getSystemResource(resourcePath);
        URI testFileUri = testFileUrl.toURI();

        Map<String, Double> results = new HashMap<>();

        Files.lines(Paths.get(testFileUri)).forEach(x -> {
            String[] strings = x.split(",");

            results.put(strings[0], Double.parseDouble(strings[1]));
        });

        return results;
    }
}

Related

  1. getFileSystem(Path file)
  2. getFileSystem(Path path)
  3. getPath(FileSystem targetFS, String fileName)
  4. isFileSystemAvailable(final Path file, final String topLevelAbsolutePath)
  5. isFileSystemAvailable2(final Path file, final String topLevelAbsolutePath)
  6. newJarFileSystem(Path jarFilePath)
  7. newOutputStream(FileSystem system, Path path)
  8. systemEnvironmentPaths()
  9. toSystemPath(String rawpath)