Java File Read by Charset getPropertiesVaule(File file, String key, Charset charset)

Here you can find the source of getPropertiesVaule(File file, String key, Charset charset)

Description

Gets properties vaule.

License

Apache License

Parameter

Parameter Description
file the file
key the key
charset the charset

Exception

Parameter Description
IOException the io exception

Return

String properties vaule

Declaration

public static String getPropertiesVaule(File file, String key, Charset charset) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.charset.Charset;

import java.util.Properties;

public class Main {
    /**//from  w w w.  j  av a  2  s. c  o m
     * Gets properties vaule.
     *
     * @param file    the file
     * @param key     the key
     * @param charset the charset
     * @return String properties vaule
     * @throws IOException the io exception
     * @Title: getPropertiesVaule
     * @Description:
     */
    public static String getPropertiesVaule(File file, String key, Charset charset) throws IOException {
        if (file == null) {
            return null;
        }
        if (charset == null) {
            charset = Charset.forName("ISO8859-1");
        }
        Properties properties = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            properties.load(fis);
        } catch (IOException e) {
            throw new IOException(e);
        } finally {

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new IOException(e);
                } finally {
                    if (properties != null) {
                        properties.clear();
                    }
                }
            }
        }
        String value = properties.getProperty(new String(key.getBytes(charset), "ISO8859-1"));
        if (properties != null) {
            properties.clear();
        }
        return value == null ? null : new String(value.getBytes("ISO8859-1"), charset);
    }

    /**
     * Gets properties vaule.
     *
     * @param file the file
     * @param key  the key
     * @return String properties vaule
     * @throws IOException the io exception
     * @Title: getPropertiesVaule
     * @Description:
     */
    public static String getPropertiesVaule(File file, String key) throws IOException {
        return getPropertiesVaule(file, key, null);
    }
}

Related

  1. getFileContents(File file, String charset)
  2. getFileEncodingCharset()
  3. getFileText(File file, Charset charset)
  4. getNumberOfNonEmptyLines(File file, Charset charset)
  5. getPatchFileCharset()
  6. getString(File file, Charset charset)
  7. getUncommentedLines(File file, Charset forName)
  8. getZipFile(File src, Charset charset)
  9. isEqual(final File document, final Charset a, final Charset b)