在applet中加载Properties文件

时间:2012-01-06 19:23:31

标签: java resources properties applet

Applets新手,我从未处理过将资源导出到jar的问题。

浏览器无法加载属性文件:

access denied ("java.io.FilePermission"
"config\en-us.properties""read")

属性文件导入如下:

enter image description here

加载属性文件的代码:

prop.load(new FileInputStream("config/en-us.properties"));

1 个答案:

答案 0 :(得分:4)

使用以下方法获取jar中属性文件的URL:

URL urlToProps = this.getClass().getResource("/config/en-us.properties");

使用URLConnection设置读取超时。

// courtesy of MyTitle 'default timeout is infinity'
URLConnection connection = urlToProps.openConnection(); 
connection.setConnectTimeout(5000); 

获取InputStream

InputStream is = connection.getInputStream();

然后使用Properties.load(InputStream)加载它。

prop.load(is);