Propeties读写问题

xyxu1982 2004-08-12 04:31:22
以下是我的代码
import java.util.*;
import java.net.*;
import java.io.*;
class Property
{
public static void main(String[] args)
{
Properties props = new Properties();
URL url = ClassLoader.getSystemClassLoader().getResource(
"test.properties");
try {
File f = new File(url.getPath());
InputStream is = new FileInputStream(f);
props.load(is);
System.out.println(props.getProperty("xy"));
System.out.println(props.getProperty("xj"));
OutputStream out=new FileOutputStream(f);
props.store(out, "xy=yinxu");
out.close();
}
catch (Exception e)
{
System.err.print(e);
}
System.out.println(props.getProperty("xy"));
System.out.println(props.getProperty("yx"));
}
}
test.property文件如下 xy=xuyin
yx=yangxin
我要的打印结果是:xuyin
yangxin
yinxu
yangxin
可是结果是;#xy=yinxu
#Thu Aug 12 16:31:07 CST 2004
yx=yangxin
xy=xuyin
怎么会得到这样的结果?请问应该怎样改才能得到我要的结果?
...全文
117 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
云物数智 2004-08-13
  • 打赏
  • 举报
回复
我想是因为你把同一个文件先打开为输出流,没有使用,然后你又把它打开为输如流,又去读取他的数据可能就不对了。
打开为out就进行读操作,在打开is进行写操作。
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
to IceCraft(心淡情浓):
如果你愿意加我QQ415009052。
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
还有个问题:如果我把
OutputStream out=new FileOutputStream(f);向上移到
InputStream is = new FileInputStream(f);下面就又不对了,请问为什么会这样?
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
谢谢你,结果是对的了^_^。
云物数智 2004-08-12
  • 打赏
  • 举报
回复
改成这样:
public class Property {
public static void main(String[] args) {
Properties props = new Properties();
URL url = ClassLoader.getSystemClassLoader().getResource(
"test.properties");
try {
File f = new File(url.getPath());
InputStream is = new FileInputStream(f);
props.load(is);
System.out.println(props.getProperty("xy"));
System.out.println(props.getProperty("yx"));
OutputStream out=new FileOutputStream(f);
props.setProperty("xy","yinxu");//此处设置xy的值
props.store(out,"mydoc");//空白内容可以写你对此文件的注释
out.close();
}
catch (Exception e)
{
System.err.print(e);
}
System.out.println(props.getProperty("xy"));
System.out.println(props.getProperty("yx"));
}
}
控制台的输出效果是:
xuyin
yangxin
yinxu
yangxin
文件内容原来是:
xy=xuyin
yx=yangxin
执行后变成:
#myDoc
#Thu Aug 12 17:48:04 CST 2004
yx=yangxin
xy=yinxu

xy的内容已修改,#号表示的意思是注释,指明了修改文件的时间和你要添加的注释。
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
各位前辈帮我改改代码吧,分不够可以再加的。
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
to IceCraft(心淡情浓):
我主要想知道怎样对property写,谢谢你帮我改改。
云物数智 2004-08-12
  • 打赏
  • 举报
回复
PropertyConfigurator.configure("log.properties");
这一行可以忽略,是载入另一个配置用的。
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
各位前辈都来改改我的代码吧,怎样才能得到我要的结果?
xyxu1982 2004-08-12
  • 打赏
  • 举报
回复
先谢谢你,我试试。
云物数智 2004-08-12
  • 打赏
  • 举报
回复
你参考着修改一下你的代码试试,good luck:)
云物数智 2004-08-12
  • 打赏
  • 举报
回复
配置文件也很简单:
mode=0
autoDelete=false
dowDownLoad=172.16.1.130
....
云物数智 2004-08-12
  • 打赏
  • 举报
回复
我是这样干的
private Properties proper;
private String mode;
PropertyConfigurator.configure("log.properties");
proper=new Properties();
try{
//载入配置文件
FileInputStream propis=new FileInputStream(new File("ftp.properties"));
proper.load(propis);
}
catch(FileNotFoundException fnfe){
logger.error("配置文件未找到:"+fnfe);
}
catch(IOException ioe){
logger.error("IOException:"+ioe.getMessage());
return;
}
mode=proper.getProperty("mode");
if(mode==null||mode.equals("")){
System.out.println("未指定上传或下载模式,默认为下载!");
mode="0";
}
System.out.println(mode);

62,623

社区成员

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

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