public static void setInfoByProperty(String id,String property,String propertyValue) throws Exception{
File file = getFile(id);
PrintWriter pw = new PrintWriter(file);
Properties ps = new Properties();
ps.setProperty(property,propertyValue);
ps.list(pw);
pw.flush();
pw.close();
}如上面方法代码,是根据id找到文件,再把输入相对属性的值写到文件中,但我这样写,每次录入文件都话刷新了上次录入的,怎么才能把新输入的追加到文件中,而不覆盖旧的内容?
解决方案 »
- web压缩成war文件在tomcat运行问题?
- 新手struts问题
- 请高手来帮我看一下,谢谢!
- 怎么制作CMS啊?
- JAXB 解析xml文件出现的错误(模拟IOC模式)
- 大家好,有使用java(jbuilder10)开发xml的朋友请进来,关于使用Xerceslmpl.jar的问题
- 为什么我的ie只能显示英文(tomcat5+jdk1.5)
- 关于session的问题,送100分
- DEBUG NewPooledConnection:294
- gson包的getAsString()与toString()问题
- 在页面使用struts2的标签,界面就报错,
- SSH注解的方式更新数据库,出现更新语句,但数据并未更新到数据库
bunles 这个是文件 BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(bunles,true)));
writer.write(“xxxx追加的内容”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(writer != null)
writer.close();
}
pro.load(new FileInputStream("readxmllasttime.porperties"));
for (Enumeration e = pro.propertyNames(); e.hasMoreElements();) {
String s = (String) e.nextElement(); // 遍历所有元素
if (s.equals(key)) {
/**如果键相同则覆盖*/
pro.setProperty(key, maxdate.getTime() + "");
} else {
/**否则就原样写入*/
pro.setProperty(s, pro.getProperty(s));
}
}
pro.store(new FileOutputStream("readxmllasttime.porperties"),"描述信息");
public static void setInfoByProperty(String id,String property,String propertyValue) throws Exception{
File file = getFile(id);
//PrintWriter pw = new PrintWriter(file);
Properties ps = new Properties();
ps.load(new FileReader(file));
for (Enumeration e = ps.propertyNames(); e.hasMoreElements();) {
String s = (String) e.nextElement(); // 遍历所有元素
if (s.equals(property)) {/**如果键相同则覆盖*/
//ps.setProperty(property,propertyValue);
} else {/**否则就原样写入*/
ps.setProperty(property,propertyValue);
}
}
ps.store(new FileWriter(file),"qqq");
}
根据你的改成这样,貌似不行!!!
执行后有文件里面有#qqq和一行时间,但属性没有~~~什么回事?
File file = getFile(id);
FileInputStream fis = new FileInputStream(file);
Properties ps = new Properties();
ps.load(fis);
ps.setProperty(property,propertyValue);
FileOutputStream fos = new FileOutputStream(file);
ps.store(fos, "");
os.close();
FileWriter可以追加,构造器中指定即可
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;/**
* 属性文件读写工具
* @author shanl
*
*/
public class ResourceUtil {
private File configFile = null;
private Properties prop = null;
public ResourceUtil(URL url){
load(url);
}
/**
* 从classpath下查找指定资源<br/>
* 以类的部署目录为根,
* 例:conf/config.properties
* @param res
* @return
*/
public static ResourceUtil getInstanceByClasspath(String res){
URL url = ClassLoader.getSystemResource(res);
return new ResourceUtil(url);
}
/**
* 从classload下查找指定资源<br/>
* 以类的部署目录为根,
* 例:conf/config.properties
* @param res
* @return
*/
public static ResourceUtil getInstanceByClassloader(String res){
URL url = ResourceUtil.class.getClassLoader().getResource(res);
return new ResourceUtil(url);
}
/**
* 从配置文件中加载
* @param url
*/
private void load(URL url){
InputStream in = null;
try{
prop = new Properties();
configFile = new File(url.toURI());
in = new FileInputStream(configFile);
prop.load(in);
}catch(Exception ex){
throw new RuntimeException(ex);
}finally{
try{
if(null!=in) in.close();
}catch(Exception ex){
}
}
}
/**
* 返回某一项属性值
* @param key
* @return 返回属性值,如果不存在或没有值则返回""
*/
public String getProperty(String key){
return prop.getProperty(key,"");
}
/**
* 设置一项属性值
* @param key
* @param value
*/
public void setProperty(String key, String value){
prop.setProperty(key, value);
}
/**
* 移除一项
* @param key
*/
public void removeProperty(String key){
prop.remove(key);
}
/**
* 将当前各属性值保存至文件
*/
public void store(){
OutputStream out = null;
try{
out = new FileOutputStream(configFile);
prop.store(out, null);
}catch(Exception ex){
throw new RuntimeException(ex);
}finally{
try{
if(null!=out) out.close();
}catch(Exception ex){
}
}
}
}