62,623
社区成员
发帖
与我相关
我的任务
分享
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());
}
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;
}
}
/**
* ���ļ�д������
* @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 + " ��д д��ɹ�!重写成功");
}
}