请教,如何向属性文件test.properties里面写文件?

qicaihuochai 2008-04-06 01:42:15
请教,如何向属性文件test.properties里面写文件?

如果不用属性文件,也可以,只要能做到:第二次存入的数据不会覆盖前面的数据就好。

我要这个代码,是想用来做JAVA邮件服务器上存储用户名和密码使用。

请各位高手将代码给我,谢谢!!
...全文
116 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2008-09-12
  • 打赏
  • 举报
回复

try{
FileInputStream fis = new FileInputStream("c:\\config.properties");
Properties pro = new Properties();
pro.load(fis);
fis.close();
pro.setProperty("name", "MyName");
pro.store(new FileOutputStream("c:\\config.properties"), "comments");
fis.close();
}catch(Exception e){
System.out.println(e.toString());
}
mayuanfei 2008-09-12
  • 打赏
  • 举报
回复
一定要好好看,我写了很长时间的

package com.adm.test2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;


public class MailServiceTest {
Document doc = null;

File file = null;

public MailServiceTest() {
SAXBuilder build = new SAXBuilder();
try {
//构建Document.
file = new File("d:\\mailpassword.xml");
doc = build.build(this.getClass().getClassLoader().getResource("mailpassword.xml"));
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 用于增加一个User的Mail信息
*/
public void appendUserMail(User user) throws Exception {
Element root = doc.getRootElement();
Element userNode = new Element("user");
userNode.setAttribute("id", ""+user.getId());
userNode.setAttribute("name", ""+user.getName());
Element mailNode = new Element("mail");
mailNode.setText(user.getMail());
Element passwordNode = new Element("mail");
passwordNode.setText(user.getPassword());
userNode.addContent(mailNode);userNode.addContent(passwordNode);
root.addContent(userNode);

//输出
XMLOutputter out = new XMLOutputter();
Format format = Format.getPrettyFormat();
out.setFormat(format);
out.output(doc,new FileOutputStream(file));


}

/**
* 获得所有的用户名和密码
* @throws Exception
*/
public List<User> getAllUsers() throws Exception{
List<User> users = new ArrayList<User>();
Element root = doc.getRootElement();
List<Element> children = root.getChildren("user");
for(Element e : children) {
User user = new User();
int id = e.getAttribute("id").getIntValue();
String name = e.getAttributeValue("name").toString();
String mail = e.getChildText("mail");
String password = e.getChildText("password");
user.setId(id);user.setName(name);
user.setMail(mail);user.setPassword(password);
users.add(user);
}
return users;
}

/**
* 安装用户Id查找一个用户
*/
public User findUserById(int id) {
return null;
}

/*下面还可以加方法,比如更改一个已经有的用户的用户名密码.等等.*/


public static void main(String[] args) {
MailServiceTest test = new MailServiceTest();
try{
List<User> allUsers = test.getAllUsers();
for(User user : allUsers) {
System.out.println(user.getId()+","+user.getName()+","+user.getMail()+","+user.getPassword());
}
test.appendUserMail(new User(333,"333","333","3333"));
List<User> allUsers1 = test.getAllUsers();
for(User user : allUsers1) {
System.out.println(user.getId()+","+user.getName()+","+user.getMail()+","+user.getPassword());
}
}catch(Exception e) {
e.printStackTrace();
}
}

static class User {
int id ;
String name;
String mail;
String password;

public User(){
}
public User(int id, String name, String mail, String password) {
this.id = id;
this.name = name;
this.mail = mail;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

public Document getDoc() {
return doc;
}

}

banditgao 2008-09-12
  • 打赏
  • 举报
回复
楼上的代码不能写入 还说写入成功
yibunengjing 2008-04-07
  • 打赏
  • 举报
回复
/**
* ���ļ�д������
* @param fileName 文件名称
* @param fromEnd T/F 是否从文本写入��ʼд
* @param str 要写入的文本
* @throws Exception
*/
private static void write(String fileName, boolean fromEnd, StringBuffer str) throws Exception {
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
if(fromEnd){
raf.seek(raf.length());
raf.writeBytes(str.toString());
System.out.println(fileName + " ��!文本插入成功");
}else{
File f = new File(fileName);
FileOutputStream fos=new FileOutputStream(f);
fos.close();
raf.writeBytes(str.toString());
System.out.println(fileName + " ��д д��ɹ�!重写成功");
}
}
hmsuccess 2008-04-06
  • 打赏
  • 举报
回复
这里以仅讨论过了:http://topic.csdn.net/u/20080327/23/9b4a61b1-6c80-4a3e-a29f-bd49be3610e7.html
hmsuccess 2008-04-06
  • 打赏
  • 举报
回复
若在文件末尾添加,就像FileWriter或FileOutputStream(String name, boolean append)中的append
设为true,若在文件中添加内容,用RandomAccessFile

sky_ccy 2008-04-06
  • 打赏
  • 举报
回复
用writer类把他打开,用write(String str)写入数据,用newline()换行
OnlyFor_love 2008-04-06
  • 打赏
  • 举报
回复
文件操作中都方法是追加到文本最后字符后面的,你查下帮助文档就可以了

62,623

社区成员

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

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