码迷,mamicode.com
首页 > 编程语言 > 详细

java代码

时间:2015-04-12 19:09:13      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

io的使用

技术分享
 1 package com.tan.io;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 
 6 class Employee{
 7     private String name;
 8     private double salary;
 9     private Date hireDay;
10     
11     public Employee(String name,double salary,int year,int month,int day){
12         this.name=name;
13         this.salary=salary;
14         GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
15         hireDay=calendar.getTime();
16     }
17     
18     public String getName(){
19         return name;
20     }
21     
22     public double getSalary(){
23         return salary;
24     }
25     
26     public Date getHireDay(){
27         return hireDay;
28     }
29     
30     public void raiseSalary(double byPersent){
31         double raise=salary*byPersent/100;
32         salary+=raise;
33     }
34 }
35 
36 public class TextFileTest {
37     public static void main(String[] args)throws IOException{
38         Employee[] staff=new Employee[3];
39         staff[0]=new Employee("Carl Cracker", 75000, 1987,12,15);
40         staff[1]=new Employee("Harry Hacker",50000,1989,10,1);
41         staff[2]=new Employee("Tony Tester",40000,1990,3,15);
42         
43         try(PrintWriter out=new PrintWriter("employee.txt","UTF-8")){
44             writeData(staff,out);
45         }
46         
47         try(Scanner in=new Scanner(
48                 new FileInputStream("employee.txt"),"UTF-8")){
49             Employee[] newStaff=readData(in);
50             for(Employee e : newStaff){
51                 System.out.println(e);
52             }
53         }
54     }
55 
56     private static Employee[] readData(Scanner in) {
57         int n=in.nextInt();
58         in.nextLine();
59         
60         Employee[] employees=new Employee[n];
61         for(int i=0;i<3;i++){
62             employees[i]=readEmployee(in);
63         }
64         return employees;
65     }
66 
67     private static Employee readEmployee(Scanner in) {
68         // TODO Auto-generated method stub
69         String line=in.nextLine();
70         String[] token=line.split("\\|");
71         String name=token[0];
72         double salary=Double.parseDouble(token[1]);
73         int year=Integer.parseInt(token[2]);
74         int month=Integer.parseInt(token[3]);
75         int day=Integer.parseInt(token[4]);
76         return new Employee(name, salary, year, month, day);
77     }
78 
79     private static void writeData(Employee[] staff, PrintWriter out) {
80         // TODO Auto-generated method stub
81         out.println(staff.length);
82         for(Employee e:staff){
83             writeEmployee(out,e);
84         }
85     }
86 
87     private static void writeEmployee(PrintWriter out, Employee e) {
88         // TODO Auto-generated method stub
89         GregorianCalendar calendar=new GregorianCalendar();
90         calendar.setTime(e.getHireDay());
91         out.println(e.getName()+"|"+e.getSalary()+"|"+calendar.get(Calendar.YEAR)+"|"+
92         (calendar.get(Calendar.MONTH+1))+"|"+calendar.get(Calendar.DAY_OF_MONTH));
93     }
94     
95     
96 }
textFile

 

java代码

标签:

原文地址:http://www.cnblogs.com/tannian/p/4420045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!