您的位置:首页 > 运维架构

ATM-PROGRAM 关于Proprties的问题

2015-10-26 18:27 363 查看
public static void turnMoney(String ToAccNo, int money){
d = new Date();
dateStr = noteDate.format(d);
Properties p = new Properties();
File f = new File("c://Account/" + ToAccNo + ".txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
if(f.exists()){

fis = new FileInputStream(f);

p.load(fis);
System.out.println(ToAccNo+ p.getProperty("name")+p.getProperty("password")+money);
Account toAcc = new Account(ToAccNo,.getProperty("name"),.getProperty("password"), money);
if(acc.getMoney() >= money){
System.out.println("转账成功!");
acc.setMoney(acc.getMoney() - money);
toAcc.setMoney(toAcc.getMoney() + money);
noteDateFile(dateStr + " 向" + toAcc.getName() + "账户转入" + money + "块");

fos = new FileOutputStream(f);

p.setProperty("accNo",toAcc.getAccNo());
p.setProperty("name", toAcc.getName());
p.setProperty("password", toAcc.getPassword());
p.setProperty("money",new Integer(toAcc.getMoney()).toString());
try {
p.store(fos, null);
} catch (IOException e) {
e.printStackTrace();
}finally{
fis.close();
fos.close();
}
}else{
System.out.println("余额不足,请确认后操作!");
}
}else{
System.out.println("对方卡号错误!");
}

} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}

问题是在Properties这儿,若要先读取文件里的属性先要将文件输入IO流加载出来,然后再用Properties的get方法,但是若在此过程的中间插入了文件输出IO流关联了文

件,则会清楚该文件里的所以内容!

比如以下的写法:

File f = new File("c://Account/" + ToAccNo + ".txt");

Properties p = new Properties();

FileInputStream fis = null;

FileOutputStream fos = null;

fis = new FileInputStream(f);

fos = new FileOutputStream(f);

p.load(fis);

那么就会清空文件里的所有内容

另外,当要深层的创建一个文件时,光用file.creatNewFile()是不行的,得先创建文件路径,也就是用new File("...").mkdirs()创建好深层的路径,然后再创建
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: