Java File Read by Charset setPropertiesVaule(File file, String key, String value, String comments, Charset charset)

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

Description

Sets properties vaule.

License

Apache License

Parameter

Parameter Description
file the file
key the key
value the value
comments the comments
charset the charset

Exception

Parameter Description
IOException the io exception

Return

String properties vaule

Declaration

public static String setPropertiesVaule(File file, String key, String value, String comments, 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.Date;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

public class Main {
    /**/*from w w w.j a  v a 2 s .  c  o  m*/
     * Sets properties vaule.
     *
     * @param file  the file
     * @param key   the key
     * @param value the value
     * @return String properties vaule
     * @throws IOException the io exception
     * @Title: setPropertiesVaule
     * @Description:
     */
    public static String setPropertiesVaule(File file, String key, String value) throws IOException {
        return setPropertiesVaule(file, key, value, file.getPath());
    }

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

    /**
     * Sets properties vaule.
     *
     * @param file     the file
     * @param key      the key
     * @param value    the value
     * @param comments the comments
     * @param charset  the charset
     * @return String properties vaule
     * @throws IOException the io exception
     * @Title: setPropertiesVaule
     * @Description:
     */
    public static String setPropertiesVaule(File file, String key, String value, String comments, Charset charset)
            throws IOException {
        if (file == null) {
            return null;
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        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) {
                fis.close();
            }
        }
        Object v = properties.setProperty(new String(key.getBytes(charset), "ISO8859-1"),
                new String(value.getBytes(charset), "ISO8859-1"));

        BufferedWriter bw = null;
        try {
            Set set = properties.entrySet();
            Iterator<Object> iterator = set.iterator();
            bw = new BufferedWriter(new FileWriter(file));
            bw.write("#" + comments);
            bw.newLine();
            bw.write("#" + new Date().toString());
            bw.newLine();
            while (iterator.hasNext()) {
                String str = iterator.next().toString();
                bw.write(new String(str.getBytes("ISO8859-1"), charset));
                bw.newLine();
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            throw new IOException(e);
        } finally {
            if (bw != null) {
                bw.close();
            }
            if (properties != null) {
                properties.clear();
            }
        }

        return v == null ? null : new String(v.toString().getBytes("ISO8859-1"), charset);
    }
}

Related

  1. open(File file, Charset charset)
  2. parseNameDelimiterValueNewLineFile( File nameDelimiterValueNewLineFile, String nameValueDelimiter, Charset charset)
  3. saveFile(File file, String content, String charsetName)
  4. saveToFile(File dest, String contents, Charset cs)
  5. setFileText(File file, Charset charset, String text)
  6. setText(File file, Charset charset, String text)
  7. stringToFile(final String s, final File f, final Charset c)
  8. stringToFile(final String string, final Path path, final Charset charset)
  9. toFiles(@Nonnull Process p, @Nonnull Charset charset)