高分求解:如何对ini文件进行写操作?

wddlqd 2006-02-09 11:00:44
我用Properties的getProperty()可以读取ini文件内的值,为什么用它的setProperty却不能对ini文件进行写操作啊?
求高手解答!谢谢
...全文
103 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mark_Chen 2006-02-09
  • 打赏
  • 举报
回复
帮顶!
wddlqd 2006-02-09
  • 打赏
  • 举报
回复
还是不知道怎么做。。。。。新手比较差
三合一 2006-02-09
  • 打赏
  • 举报
回复
apache上面有一个小工具可以用,叫做configure,专门用来帮助解决读写配置文件的问题,各种配置文件都可以解决
TinyJimmy 2006-02-09
  • 打赏
  • 举报
回复
SetProperty只能修改内存中的内容, 需要写到文件中才行.

自己写一个函数, 也就几十行的代码
wddlqd 2006-02-09
  • 打赏
  • 举报
回复
帮帮忙啊!急。
wddlqd 2006-02-09
  • 打赏
  • 举报
回复
谢谢,我自己写了个类解决了,没有用Properties。
飞翔的大麦茬 2006-02-09
  • 打赏
  • 举报
回复
不好意思漏写了点,全帖出来

public class PropertiesConfiguration {
public String path = "" ;
private Properties properties;
public PropertiesConfiguration(String file){
//String url = this.getClass().getClassLoader().getResource("config.properties").toString();
this.path = file;
properties = new Properties();
}


public String[] getProperty(String[] key) {
String temp[] = new String[key.length];
try {
FileInputStream in = new FileInputStream(path);
properties.load(in);
for(int i= 0 ; i < key.length ; i++){

temp[i] = properties.getProperty(key[i]).trim();
}

in.close();
}
catch (IOException e) {
}

return temp ;
}

public void setProperty(String[] key, String[] value) {

try {
FileOutputStream out = new FileOutputStream(path);
PrintStream ps = new PrintStream(out);
for(int i = 0; i<key.length ; i++){
//System.out.println(value[i]);
properties.setProperty(key[i], value[i]);
}

properties.list(ps);
out.close();
ps.close();
}
catch (IOException e) {
System.out.println(e.toString());
}

}

}
飞翔的大麦茬 2006-02-09
  • 打赏
  • 举报
回复
类似下面的写法

FileOutputStream out = new FileOutputStream(path);
PrintStream ps = new PrintStream(out);
properties.setProperty(key, value);
properties.list(ps);
out.close();
ps.close();
mengxiaoyong 2006-02-09
  • 打赏
  • 举报
回复
你从ini文件转成文件流FileInputStream然后用
public void load(InputStream inStream)
throws IOException
方法就可以加载这个文件,进行相应的操作之后调用
public void store(OutputStream out,
String comments)
throws IOException
方法进行保存,首先你要有一个FileOutputStream流,你让FileInputStream和FileOutputStream流都指向同一个文件就可以了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧