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()创建好深层的路径,然后再创建
相关文章推荐
- 从此使用linux系统,但是QQ是必不可少的!!该篇文章方法成功!!!已验证!!!!!
- 《Linux Shell》笔记之正则表达式
- 《Linux Shell》笔记之grep
- 《Linux Shell》笔记之dig
- HDOJ 题目1195 Open the Lock(双向BFS)
- linux_增加用户组_删除用户
- kali——查看版本
- windows下安装linux双系统
- 理解本真的REST架构风格
- Sqoop 初识
- Linux LVM扩展和收缩,修改swap大小
- 做网站的时候
- Linux防火墙指定端口范围
- .net 大型分布式电子商务架构说明
- MAC 安装 openvc
- linux(ubuntu14.04)+GPU+cuda6.5+caffe+openCV2.4.9+matlab2014a+python的新手配置(修改版)
- 搜索引擎有哪些分类?(外贸购物网站建设)
- Linux下Service守护进程开发和Shell脚本控制Daemon
- Linux下grep查询包含某个关键字的上下文信息
- linux 整理-1