如何将内容写入自定义java属性文件

时间:2013-12-02 13:44:23

标签: java properties

这是自定义java属性文件,它保留元素的插入顺序。当我运行以下代码时,属性不会写入文件。该文件的内容只是:

#Mon Dec 02 13:39:55 GMT 2013

我是否需要覆盖其他方法?这不应该工作,因为自定义属性文件正在扩展主要的属性文件,所以所有未被覆盖的方法应该以相同的方式运行吗?

不会抛出异常

代码:

package com.test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class LinkedProperties extends Properties{


    private static final long serialVersionUID = 1L;

    private Map<Object, Object> linkMap = new LinkedHashMap<Object,Object>();

    @Override
    public synchronized Object put(Object key, Object value){
        return linkMap.put(key, value);
    }

    @Override
    public synchronized boolean contains(Object value){
        return linkMap.containsValue(value);
    }

    @Override
    public boolean containsValue(Object value){
        return linkMap.containsValue(value);
    }

    @Override
    public synchronized Enumeration<Object> elements(){
        throw new UnsupportedOperationException(
          "Enumerations are so old-school, don't use them, "
        + "use keySet() or entrySet() instead");
    }

    @Override
    public Set<java.util.Map.Entry<Object, Object>> entrySet(){
        return linkMap.entrySet();
    }

    @Override
    public synchronized void clear(){
        linkMap.clear();
    }

    @Override
    public synchronized boolean containsKey(Object key){
        return linkMap.containsKey(key);
    }

    @Override
    public synchronized Object setProperty(String key, String value) {
        return put(key, value);
    }

}

package com.test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Runner {

    public static void main(String args[]){

        LinkedProperties l = new LinkedProperties();
        l.put("test", "test");
        try {
            l.store(new FileOutputStream("c:/text.txt"),null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

更新:

感谢raphw&amp;提供的答案。 frostjogla我现在使用这个自定义类按顺序编写属性:

package com.test;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class LinkedProperties extends Properties {

    private static final long serialVersionUID = 1L;

    private Map<Object, Object> linkMap = new LinkedHashMap<Object, Object>();

    @Override
    public synchronized Object put(Object key, Object value) {
        return linkMap.put(key, value);
    }

    @Override
    public synchronized boolean contains(Object value) {
        return linkMap.containsValue(value);
    }

    @Override
    public boolean containsValue(Object value) {
        return linkMap.containsValue(value);
    }

    @Override
    public synchronized Enumeration<Object> elements() {
        throw new UnsupportedOperationException(
                "Enumerations are so old-school, don't use them, "
                        + "use keySet() or entrySet() instead");
    }

    @Override
    public Set<java.util.Map.Entry<Object, Object>> entrySet() {
        return linkMap.entrySet();
    }

    @Override
    public synchronized void clear() {
        linkMap.clear();
    }

    @Override
    public synchronized boolean containsKey(Object key) {
        return linkMap.containsKey(key);
    }

    @Override
    public synchronized Object setProperty(String key, String value) {
        return put(key, value);
    }

    public synchronized void store(OutputStream out, String comments)
            throws IOException {
        BufferedWriter awriter;
        awriter = new BufferedWriter(new OutputStreamWriter(out, "8859_1"));
        if (comments != null)
            writeln(awriter, "#" + comments);
        writeln(awriter, "#" + new Date().toString());
        for (Iterator<java.util.Map.Entry<Object, Object>> e = linkMap.entrySet().iterator(); e.hasNext();) {
            java.util.Map.Entry<Object, Object> entry = (java.util.Map.Entry<Object, Object>) e.next();

            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            key = saveConvert(key, true);

            /*
             * No need to escape embedded and trailing spaces for value, hence
             * pass false to flag.
             */
            val = saveConvert(val, false);
            writeln(awriter, key + "=" + val);
        }
        awriter.flush();
    }

    private static void writeln(BufferedWriter bw, String s) throws IOException {
        bw.write(s);
        bw.newLine();
    }

    private String saveConvert(String theString, boolean escapeSpace) {
        int len = theString.length();
        int bufLen = len * 2;
        if (bufLen < 0) {
            bufLen = Integer.MAX_VALUE;
        }
        StringBuffer outBuffer = new StringBuffer(bufLen);

        for (int x = 0; x < len; x++) {
            char aChar = theString.charAt(x);
            // Handle common case first, selecting largest block that
            // avoids the specials below
            if ((aChar > 61) && (aChar < 127)) {
                if (aChar == '\\') {
                    outBuffer.append('\\');
                    outBuffer.append('\\');
                    continue;
                }
                outBuffer.append(aChar);
                continue;
            }
            switch (aChar) {
            case ' ':
                if (x == 0 || escapeSpace)
                    outBuffer.append('\\');
                outBuffer.append(' ');
                break;
            case '\t':
                outBuffer.append('\\');
                outBuffer.append('t');
                break;
            case '\n':
                outBuffer.append('\\');
                outBuffer.append('n');
                break;
            case '\r':
                outBuffer.append('\\');
                outBuffer.append('r');
                break;
            case '\f':
                outBuffer.append('\\');
                outBuffer.append('f');
                break;
            case '=': // Fall through
            case ':': // Fall through
            case '#': // Fall through
            case '!':
                outBuffer.append('\\');
                outBuffer.append(aChar);
                break;
            default:
                if ((aChar < 0x0020) || (aChar > 0x007e)) {
                    outBuffer.append('\\');
                    outBuffer.append('u');
                    outBuffer.append(toHex((aChar >> 12) & 0xF));
                    outBuffer.append(toHex((aChar >> 8) & 0xF));
                    outBuffer.append(toHex((aChar >> 4) & 0xF));
                    outBuffer.append(toHex(aChar & 0xF));
                } else {
                    outBuffer.append(aChar);
                }
            }
        }
        return outBuffer.toString();
    }

    private static char toHex(int nibble) {
        return hexDigit[(nibble & 0xF)];
    }

    /** A table of hex digits */
    private static final char[] hexDigit = { '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

}

2 个答案:

答案 0 :(得分:1)

您应该覆盖方法keys

这是原始方法:

public synchronized Enumeration<K> keys() {
    return this.<K>getEnumeration(KEYS);
}

将JDK源附加到IDE以进行调试应用程序。

答案 1 :(得分:1)

如果您正在查看Properties的实现,可以在store方法中找到答案,该方法负责将属性写入输出流。这是一段摘录:

bw.write("#" + new Date().toString());
bw.newLine();
for (Enumeration e = keys(); e.hasMoreElements();) { // Here things go wrong!
  String key = (String)e.nextElement();
  String val = (String)get(key);
  key = saveConvert(key, true, escUnicode);
  val = saveConvert(val, false, escUnicode);
  bw.write(key + "=" + val);
  bw.newLine();
}

通过此方法,您可以了解需要提供哪些功能才能将属性写入输出文件。在内部,Properties调用keys超级Properties中定义的Hashtable方法,并且您不会覆盖该方法。因为您正在拦截填充PropertiesHashtable对象的原始集合的所有方法,所以此集合仍为空。因此,仍然指向此keys,空集合将返回一个空集,并且您找不到任何输出,而不是上述代码段第一行的输出。