Java 核心技术(第八版)卷II:高级知识:例1-1P14TextFileTest

本程序实现了 把 雇员类的 写入到文件中,以及从文件中读取数据形成雇员类数组。

import java.util.Date;
import java.util.GregorianCalendar;
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 Cracker",75000,1987,12,15);
        staff[1]= new Employee("Harry Hacker",50000,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 employedd records
            System.out.println("I will print the staff array:");
            for(Employee e:newstaff)
                System.out.println(e);
        }
        catch (IOException exception)
        {
            exception.printStackTrace();//Prints this throwable and its backtrace to the standard error stream.
        }

    }
    //write all employees in an array to a print writer
    private static void writeData(Employee[] employees,PrintWriter out) throws IOException
    {
        //write number of employee
        out.println(employees.length);
        for(Employee e:employees)
            e.writeData(out);
    }
    //Reads an array of employees from a scanner
    private static Employee[] readData(Scanner in)
    {
        //retrieve the array size
        int n=in.nextInt();//Scans the next token of the input as an int.  返回值: the int scanned from the input
        in.nextLine();//consume newline
        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 raiseSalary(double byPercent)
    {
        double raise=salary*byPercent/100;
        salary+=raise;
    }
    public String toString()
    {
        return getClass().getName()+"[name="+name+",salaray="+salary+",hireDay="+hireDay+"]";
    }
    //write employee data to a printer wireter
    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
    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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值