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()建立好深層的路徑,而後再建立