Java Properties Load from File loadProperties(Map map, File file, String encoding)

Here you can find the source of loadProperties(Map map, File file, String encoding)

Description

load Properties

License

Open Source License

Declaration

public static void loadProperties(Map<String, String> map, File file, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww  w  .ja v a2s  .  com
 * Copyright (c) 2001-2011 Convertigo SA.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see<http://www.gnu.org/licenses/>.
 *
 * $URL$
 * $Author$
 * $Revision$
 * $Date$
 */

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

import java.util.Map;

public class Main {
    public static void loadProperties(Map<String, String> map, File file, String encoding) throws IOException {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
            String key = null;
            String line;
            int cpt = 1;
            while ((line = br.readLine()) != null) {
                if (line.length() != 0) {
                    if (cpt % 3 == 1) {
                        key = line;
                    } else if (cpt % 3 == 2) {
                        map.put(key, line);
                    } else {
                        throw new IOException("The line number " + cpt + " must be empty (" + line + ")");
                    }
                } else {
                    if (cpt % 3 != 0) {
                        throw new IOException(
                                "The line number " + cpt + " must not be empty (last key =" + key + ")");
                    }
                }
                cpt++;
            }
        } finally {
            if (br != null)
                br.close();
        }
    }
}

Related

  1. loadProperties(final File propertiesFile)
  2. loadProperties(final String file)
  3. loadProperties(final String fileName)
  4. loadProperties(final ZipFile file, final ZipEntry ze)
  5. loadProperties(JarFile enclosedJarFile, String jarEntryPath)
  6. loadProperties(ProcessingEnvironment env, String fileName)
  7. loadProperties(Properties properties, File file)
  8. loadProperties(Properties properties, File propertyFile)
  9. loadProperties(Properties properties, File propertyFile)