一个类似properties文件内容的字符串,如何使用properties类操作? String-->bytes-->Stream-->Properties 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你说的是不是显示一段文章,你直接从DB里读取,然后显示不就可以了。为什么非要和properties撤上关系呢? 不是显示文章。 是将 此字串转为另一个数据库中的字段。<资源> <资源卷次> <年鉴类别> <标题> <正文> 分别为要写入别一个数据库的中每个字段 其实就是流的操作如: String s = "a=aaaa\r\nb=bbbb\r\nc=cccc"; try { Properties pt = new Properties(); pt.load(new StringBufferInputStream(s)); System.out.println(pt.getProperty("a")); System.out.println(pt.getProperty("b")); System.out.println(pt.getProperty("c")); } catch (IOException e) { e.printStackTrace(); } Properties类的load方法需要一个inputStream作为参数,你只需要将String用相应的InputStream wrap一下就可以了,基本上如 kingfish(八百里秦川@龙城异客)所说!只不过,StringBufferInputStream类已经Deprecated了! 下面的例子不论是什么类型文件名都可以,只要是文件内容格式是<资源>=上海统计年鉴<资源卷次>=2002<年鉴类别>=A<标题>=编者说明。每一行对应一个key和value,和*.properties的文件内容一样格式Properties properties = new Properties();File file = new File(filename);BufferedReader bufReader = new BufferedReader(new FileReader(file));//分析数据String first = null;while ((first = processor.readLine()) != null) { index = first.indexOf("="); if (index <= 0) continue; properties.setProperty(first.substring(0, index), first.substring(index + 1, first.length()));} 其实从Property文件读取信息,尤其是配置信息还是用得很多,很多时候也是很方便的。我也举个例子吧。 File file = new File(filename);BufferedReader bufReader = new BufferedReader(new FileReader(file));String first;Properties properties = new Properties();while ((frist = bufReader.readLine()) != null) { index = first.indexOf("="); if (index <= 0) continue; properties.setProperty(first.substring(0, index), first.substring(index + 1, first.length()));} 考虑到中文和deprecated问题,建议用ByteArrayInputStream 一般的Property文件的格式都是 什么什么=什么什么,注释行以"#"开头,比如:#这是一个props文件例子(test.props)username=jk3278jkpassword=xxxxxxxx#用户类型usertype=admin#部门department=sales然后在程序中:Properties p = new Properties();try { p.load(new FileInputStream("test.props"));} catch(IOException ioe) {}String username = p.getProperty("username");String password = p.getProperty("password");String usertype = p.getProperty("usertype");String department = p.getProperty("department");基本上根楼上说的差不多。 Properties类的load方法接受的是InputStream,所以要把String作为Properties文件来进行load的话可以在这里想想办法,把String转换成InputStream或者其子类。 关键就是如何把string 转成 inputStream? ??知道吗? 以前有一个StringBufferInputStream,不过已经deprecated了。再找找。 如果是英文的,直接用ByteArrayInputStream也可以,中文还需要多做点工作。 父页面设置按钮问题和获取值得问题 axis Service接收不同参数类型 如何判断两个EL表达式相等。。。。 struts2 上传问题 关于二级联动的实现 prepareStatement 中的sql语句 为什么不能用like %?% 模糊查询呢? JSP初学者的疑惑(二) 编程篇(2)实时显示 jsp网站开发如何实现敏感词的屏蔽 用URLConnection连接到一个servlet,传递参数时(get方法)的中文问题 怎样在WEBLOGIC6.0下调试SERVLET?? 字符集的问题!!急!!!!!!!!!!! 我刚发的帖子怎么找不着了?
<资源> <资源卷次> <年鉴类别> <标题> <正文> 分别为要写入别一个数据库的中每个字段
如:
String s = "a=aaaa\r\nb=bbbb\r\nc=cccc";
try {
Properties pt = new Properties();
pt.load(new StringBufferInputStream(s));
System.out.println(pt.getProperty("a"));
System.out.println(pt.getProperty("b"));
System.out.println(pt.getProperty("c"));
}
catch (IOException e) {
e.printStackTrace();
}
<资源>=上海统计年鉴
<资源卷次>=2002
<年鉴类别>=A
<标题>=编者说明
。
每一行对应一个key和value,和*.properties的文件内容一样格式Properties properties = new Properties();
File file = new File(filename);
BufferedReader bufReader = new BufferedReader(new FileReader(file));
//分析数据
String first = null;
while ((first = processor.readLine()) != null) {
index = first.indexOf("=");
if (index <= 0) continue;
properties.setProperty(first.substring(0, index), first.substring(index + 1, first.length()));
}
BufferedReader bufReader = new BufferedReader(new FileReader(file));
String first;
Properties properties = new Properties();
while ((frist = bufReader.readLine()) != null) {
index = first.indexOf("=");
if (index <= 0) continue;
properties.setProperty(first.substring(0, index), first.substring(index + 1, first.length()));
}
建议用ByteArrayInputStream
username=jk3278jk
password=xxxxxxxx
#用户类型
usertype=admin
#部门
department=sales然后在程序中:
Properties p = new Properties();
try {
p.load(new FileInputStream("test.props"));
} catch(IOException ioe) {}
String username = p.getProperty("username");
String password = p.getProperty("password");
String usertype = p.getProperty("usertype");
String department = p.getProperty("department");基本上根楼上说的差不多。