FileInputStream propertiesFile = null;
Properties prop = new Properties();
String s = "小明";
s = new String(s.getBytes("ISO-8859-1"),"gb2312")
prop.setProperty("name", s);  // ?????? 如何转码才能正常显示呢?
prop.store(new FileOutputStream("test.txt"), "myinfo");

解决方案 »

  1.   

    要从根本上面解决问题,必须重写Properties类或者提供一个Wrapper类,提供带有编码方式的save/load方法。因为Properties方法里面硬编码了ISO_8859_1,所以在外面是没有办法的。
    提供一个封装类也不太难。继承一下,增加Save(OutputStream output,String header,String Encode)方法即可。
      

  2.   

    是的,因为Properties类里定义的编码固定使用ISO_8859_1,像moumouren(某某人) 说的那样转一下就行了
      

  3.   

    to  moumouren(某某人) 
    这个我试过了呀,但是出错。
    s = new String(s.getBytes("ISO-8859-1"),"GBK")倒是可以。
    可写进去的打开一看还是“??”
      

  4.   

    WQmeng(*耶*)能不能不你的观点说的详细一些,具体做法个出来嘛!
    小弟在这里先谢谢拉!
      

  5.   

    重新写了Properties类,调用的Demo如下:关键修改点:去掉了convert函数,因此会带来的问题,我还不太明确。import java.io.*;
    import PropertiesWrapper;
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */public class test {
      public test() {
      }  public static void main(String[] args) {      try {
            FileOutputStream out = new FileOutputStream("a.txt");
            PropertiesWrapper w = new PropertiesWrapper();        w.setProperty("name", "正确的理论");
            w.setProperty("sex", "每周一次");
            w.store(out, "a header", "GB2312");
    //        w.store(out, "a header");
            out.flush();
            out.close();
          }
          catch (IOException ex) {
            ex.printStackTrace();
          }      try {
            FileInputStream in = new FileInputStream("a.txt");
            PropertiesWrapper w = new PropertiesWrapper();
            w.load(in, "GB2312");
    //        w.load(in);
            System.out.println(w.getProperty("name"));
            System.out.println(w.getProperty("sex"));
            in.close();
          }
          catch (IOException ex1) {
            ex1.printStackTrace();
          }  }}
      

  6.   

    package aaaaaa.util;import java.io.*;
    import java.util.*;
    /**
     * The <code>Properties</code> class represents a persistent set of
     * properties. The <code>Properties</code> can be saved to a stream
     * or loaded from a stream. Each key and its corresponding value in
     * the property list is a string.
     * <p>
     * A property list can contain another property list as its
     * "defaults"; this second property list is searched if
     * the property key is not found in the original property list.
     * <p>
     * Because <code>Properties</code> inherits from <code>Hashtable</code>, the
     * <code>put</code> and <code>putAll</code> methods can be applied to a
     * <code>Properties</code> object.  Their use is strongly discouraged as they
     * allow the caller to insert entries whose keys or values are not
     * <code>Strings</code>.  The <code>setProperty</code> method should be used
     * instead.  If the <code>store</code> or <code>save</code> method is called
     * on a "compromised" <code>Properties</code> object that contains a
     * non-<code>String</code> key or value, the call will fail.
     * <p>
     * <a name="encoding"></a>
     * When saving properties to a stream or loading them from a stream, the
     * ISO 8859-1 character encoding is used. For characters that cannot be directly
     * represented in this encoding,
     * <a href="http://java.sun.com/docs/books/jls/html/3.doc.html#100850">Unicode escapes</a>
     * are used; however, only a single 'u' character is allowed in an escape sequence.
     * The native2ascii tool can be used to convert property files to and from
     * other character encodings.
     *
     * @see <a href="../../../tooldocs/solaris/native2ascii.html">native2ascii tool for Solaris</a>
     * @see <a href="../../../tooldocs/windows/native2ascii.html">native2ascii tool for Windows</a>
     *
     * @author  Arthur van Hoff
     * @author  Michael McCloskey
     * @version 1.64, 06/26/00
     * @since   JDK1.0
     */
    public
    class PropertiesWrapper extends Hashtable {
        /**
         * use serialVersionUID from JDK 1.1.X for interoperability
         */
         private static final long serialVersionUID = 4112578634029874840L;    /**
         * A property list that contains default values for any keys not
         * found in this property list.
         *
         * @serial
         */
        protected PropertiesWrapper defaults;    /**
         * Creates an empty property list with no default values.
         */
        public PropertiesWrapper() {
            this(null);
        }    /**
         * Creates an empty property list with the specified defaults.
         *
         * @param   defaults   the defaults.
         */
        public PropertiesWrapper(PropertiesWrapper defaults) {
            this.defaults = defaults;
        }
      

  7.   

        public synchronized Object setProperty(String key, String value) {
            return put(key, value);
        }    private static final String keyValueSeparators = "=: \t\r\n\f";    private static final String strictKeyValueSeparators = "=:";    private static final String specialSaveChars = "=: \t\r\n\f#!";    private static final String whiteSpaceChars = " \t\r\n\f";
        public synchronized void load(InputStream inStream) throws IOException {
          load(inStream,"8859_1");
        }
        public synchronized void load(InputStream inStream,String Encode) throws IOException {        BufferedReader in = new BufferedReader(new InputStreamReader(inStream, Encode));
            while (true) {
                // Get next line
                String line = in.readLine();
                if (line == null)
                    return;            if (line.length() > 0) {
                    // Continue lines that end in slashes if they are not comments
                    char firstChar = line.charAt(0);
                    if ((firstChar != '#') && (firstChar != '!')) {
                        while (continueLine(line)) {
                            String nextLine = in.readLine();
                            if(nextLine == null)
                                nextLine = "";
                            String loppedLine = line.substring(0, line.length()-1);
                            // Advance beyond whitespace on new line
                            int startIndex=0;
                            for(startIndex=0; startIndex<nextLine.length(); startIndex++)
                                if (whiteSpaceChars.indexOf(nextLine.charAt(startIndex)) == -1)
                                    break;
                            nextLine = nextLine.substring(startIndex,nextLine.length());
                            line = new String(loppedLine+nextLine);
                        }                    // Find start of key
                        int len = line.length();
                        int keyStart;
                        for(keyStart=0; keyStart<len; keyStart++) {
                            if(whiteSpaceChars.indexOf(line.charAt(keyStart)) == -1)
                                break;
                        }                    // Blank lines are ignored
                        if (keyStart == len)
                            continue;                    // Find separation between key and value
                        int separatorIndex;
                        for(separatorIndex=keyStart; separatorIndex<len; separatorIndex++) {
                            char currentChar = line.charAt(separatorIndex);
                            if (currentChar == '\\')
                                separatorIndex++;
                            else if(keyValueSeparators.indexOf(currentChar) != -1)
                                break;
                        }                    // Skip over whitespace after key if any
                        int valueIndex;
                        for (valueIndex=separatorIndex; valueIndex<len; valueIndex++)
                            if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1)
                                break;                    // Skip over one non whitespace key value separators if any
                        if (valueIndex < len)
                            if (strictKeyValueSeparators.indexOf(line.charAt(valueIndex)) != -1)
                                valueIndex++;                    // Skip over white space after other separators if any
                        while (valueIndex < len) {
                            if (whiteSpaceChars.indexOf(line.charAt(valueIndex)) == -1)
                                break;
                            valueIndex++;
                        }
                        String key = line.substring(keyStart, separatorIndex);
                        String value = (separatorIndex < len) ? line.substring(valueIndex, len) : "";                    // Convert then store key and value
                        key = loadConvert(key);
                        value = loadConvert(value);
                        put(key, value);
                    }
                }
            }
        }
      

  8.   

    同意:
    是的,因为Properties类里定义的编码固定使用ISO_8859_1,像moumouren(某某人) 说的那样转一下就行了不好意思,偶只试过读的情况,没注意写的情况,以为和读一样处理就行了。
      

  9.   

    Properties里面的一个方法作用:
    saveConvert
    loadConvert造成的。不知道如果取消掉的话会造成怎么样的影响
      

  10.   

    to teva(用正确的理论引导人)
    万分感谢!!!请到下面的帖子回复一下,再给加些分。
    http://expert.csdn.net/Expert/topic/1929/1929691.xml?temp=.1450006
    只是你改过的程序好像不全,但我明白了。
    多谢!每周一次:)