- 浏览: 148878 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (110)
- CoreJava (18)
- 待解决 (1)
- 数据结构 (3)
- 科普 (2)
- 网站 (1)
- DataBase (8)
- Access (1)
- Asp (1)
- JSP (1)
- 操作系统 (8)
- Tech (1)
- Linux (11)
- Career (5)
- MongoDB (1)
- Embedded (1)
- JavaScript (1)
- UltraIso (1)
- Linux命令 (1)
- DesignPattern (1)
- Ruby (13)
- Vim (1)
- 自考 (2)
- Github (5)
- zrProject (1)
- Emacs (4)
- Math (3)
- Ajax (1)
- 没看懂 (1)
- HTML (1)
- Philosophy (1)
- 软件 (1)
- 面试 (1)
- 考试报名 (3)
- Regex (1)
- 日语 (1)
- 生活 (1)
最新评论
package SectionIO; import java.io.*; import java.util.*; public class TextFileTest { public static void main(String[] args) { Employee[] staff=new Employee[3]; staff[0]=new Employee("Carl Hacker",75000,1987,12,15); staff[1]=new Employee("Harry Hacker",5000,1989,10,1); staff[2]=new Employee("Tony Tester",40000,1990,3,15); try{ //save all employee records to the file employee.dat PrintWriter out = new PrintWriter("employee.dat"); writeData(staff,out); out.close(); //retrieve all records into a new array Scanner in = new Scanner(new FileReader("employee.dat")); Employee[] newStaff=readData(in); in.close(); //print the newly read employee records for(Employee e:newStaff){ System.out.println(e); } }catch(IOException exception){ exception.printStackTrace(); } } /** * Writes all employees in an array to a print writer * @param employees an array of employees * @param out a print writer */ private static void writeData(Employee[] employees,PrintWriter out)throws IOException{ //write number of employees out.println(employees.length); for(Employee e :employees){ e.writeData(out); } } /** * Reads an array of employees from a scanner * @param in the scanner * @return the array of employees */ private static Employee[] readData(Scanner in){ //retrieve the array size int n=in.nextInt(); in.nextLine(); Employee[] employees=new Employee[n]; for(int i=0;i<n;i++){ employees[i]=new Employee(); employees[i].readData(in); } return employees; } } class Employee{ public Employee(){ } public Employee(String n,double s,int year,int month,int day){ name=n; salary=s; GregorianCalendar calendar=new GregorianCalendar(year,month-1,day); hireDay=calendar.getTime(); } public String getName(){ return name; } public double getSalary(){ return salary; } public Date getHireDay(){ return hireDay; } public void raseSalary(double byPercent){ double raise=salary * byPercent/100; salary+=raise; } public String toString(){ return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]"; } /** * Writes employee data to a print writer * @param out the print writer */ public void writeData(PrintWriter out){ GregorianCalendar calendar = new GregorianCalendar(); calendar.setTime(hireDay); out.println(name+"|"+salary+"|"+calendar.get(Calendar.YEAR)+"|"+(calendar.get(Calendar.MONTH)+1)+"|"+calendar.get(Calendar.DAY_OF_MONTH)); } /** * Reads employee data from a buffered reader * @param in the scanner */ public void readData(Scanner in){ String line = in.nextLine(); String[] tokens=line.split("\\|"); name = tokens[0]; salary=Double.parseDouble(tokens[1]); int y=Integer.parseInt(tokens[2]); int m=Integer.parseInt(tokens[3]); int d=Integer.parseInt(tokens[4]); GregorianCalendar calendar=new GregorianCalendar(y,m-1,d); hireDay=calendar.getTime(); } private String name; private double salary; private Date hireDay; }
发表评论
-
利用反射实现ORM
2012-03-28 13:37 986http://royzhou1985.iteye.com ... -
Eclipse插件安装
2012-03-28 09:29 906装载:http://gooss.org/are-two- ... -
Date & Calendar 类的使用
2012-03-27 14:19 664Java 语言的Calendar(日历),Date(日期 ... -
Java文件流 编码问题 - 读取文件时指定字符编码
2012-03-25 22:04 11536折磨了一天的问题,终于在黄昏的时候解决了,现在一吐为 ... -
打jar包
2012-02-04 17:12 747其实JAR文件的格式是ZIP ... -
JavaBean - Bound属性
2012-02-04 15:54 9984.4.3 Bound属性 Bound属性表示当该种 ... -
怎样在程序里获得一个空指针?
2012-01-28 15:56 6696.2 怎样在程序里获 ... -
制作可执行的Jar包
2012-01-22 16:35 737通常有两种,一种是制 ... -
Thread Local
2012-01-22 10:28 753What is Thread Local? Threa ... -
Jni介绍
2011-10-26 19:33 590http://baike.baidu.com/view/127 ... -
获取外网IP地址
2011-10-26 18:47 904访问 http://checkip.dyndns.org/ -
this 与 getSource() 细节
2011-10-14 19:33 702一个对象实例化之后,在它的方法体中出现的this就是指自身。a ... -
static变量生存周期
2011-10-13 22:38 932标准规定static变量保证在第一次使用前初始化,但是并不保证 ... -
String 引用类型与基本类型区别
2011-10-13 22:20 809String a="A"; Stri ... -
为什么会有serialVersionUID
2011-10-13 21:17 623java文件中为什么会有s ... -
抽象类与接口的区别
2011-10-09 11:30 550首先,我们来看一下抽象类的概念,java编程思想中说“万物皆对 ... -
transient用法
2011-09-19 11:16 804Java的serialization提供了一种持久化对象实例的 ...
相关推荐
一本通2060【例1.1】计算机输出
C语言程序设计例1.1
base.apk.1.1.1.1.1.1.1.1.apk.1.1.1.1
app-debug.apk.1.1.1.1.1.1.1.1.1.1
BlueApp.apk.1.1.1.1.1.1
安卓客户端.apk.1.1.1.1.1.1
安卓手机安装包.apk.1.1.1.1.1.1
FPJC.apk.1.1.1.1.1.1
4_base.apk.1.1.1.1.1.1
AWS D1.1-D1.1M-2008是美国焊接学会(AWS)制定的钢结构焊接规范的第21版,该规范在2008年7月2日被美国标准学会批准。该标准主要针对的是常用碳素钢和低合金结构钢的任何类型结构的焊接要求,并被广泛应用于建筑和...
base.apk.1.1.1.1
base.apk.1.1.1.1.1.1.1
360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒1.1双引擎360杀毒...
app-release.apk.1.1.1.1
dmq-release.apk.1.1.1.1.1.1.1
base.apk.1.1.1.1.1.1
Cloudflare_1.1.1.1.apk.cab
__UNI__5BBCA13__20220913192203.apk.1.1.1.1.1.1
5_base.apk.1.1.1.1