日期:2014-05-17  浏览次数:20763 次

读取.properties文件时出错
我在web工程下读取.properties配置文件时在这个简单的读取测试代码中
public static void main(String[] args) throws IOException {
Properties prop=new Properties();
FileInputStream fis=new FileInputStream("D:/constant.properties");
prop.load(fis);
Constant.setFTPIP(prop.getProperty("FTPIP"));
print();
}
print函数是简单的一个打印出来Constant类中FTPIP的值为什么总是报Exception in thread "main" java.lang.IllegalArgumentException: Malformed \uxxxx encoding.
at java.util.Properties.loadConvert(Properties.java:552)
at java.util.Properties.load0(Properties.java:375)
at java.util.Properties.load(Properties.java:325)
at Configure.PropertyUtil.main(PropertyUtil.java:39)
这样的错误,在线等,各位大侠帮忙看一下


------解决方案--------------------
package test;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.sinosoft.platform.framework.logger.BaseLogger;

public class Properties {
private static String BUNDLE_NAME = "System";

private static ResourceBundle RESOURCE_BUNDLE;
private static Logger _logger = BaseLogger.getPlatformLogger();
public Properties(String BUNDLE_NAME) {
this.BUNDLE_NAME = BUNDLE_NAME;
}

public static String getString(String key) {
try {
return ResourceBundle.getBundle(BUNDLE_NAME).getString(key);
} catch (MissingResourceException e) {
e.printStackTrace();
return '!' + key + '!';
}
}

public static List getString() {
List list = new ArrayList();
HashMap[] map = new HashMap[1000];
RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
int i = 0;
Enumeration<String> props = RESOURCE_BUNDLE.getKeys();
while (props.hasMoreElements()) {
map[i] = new HashMap();
String key = props.nextElement();
String value = RESOURCE_BUNDLE.getString(key);
map[i].put("key", key);
map[i].put("value", value);
list.add(i, map[i]);
i++;
}
return list;
}

public static void main(String[] args) {
// Properties p = new Properties("System");
// List s = p.getString();
// System.out.println(p.getString("A1"));
_logger.info(Properties.getString());
_logger.info(Properties.getString("3"));
// CProperties cp = new CProperties("server.properties");
// System.out.println(cp.getKey("server.properties"));
}
}
这是个读取properties的类,lz看看吧
读取文件为src下的System.properties
------解决方案--------------------
你的properties文件不对,在\u后面必须是0-9或a-f或A-F,不允许有其他字母,否则报Malformed \\uxxxx encoding.