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();spa
FileInputStream fis = null;orm
FileOutputStream fos = null;get
fis = new FileInputStream(f);it
fos = new FileOutputStream(f);io
p.load(fis);form
那么就会清空文件里的全部内容
另外,当要深层的建立一个文件时,光用file.creatNewFile()是不行的,得先建立文件路径,也就是用new File("...").mkdirs()建立好深层的路径,而后再建立