import java.io.*;
import java.time.LocalDate;
import java.util.Scanner;

public class Test {
    public static void main(String[] args){
//        TextFileTest.test();
//        RandomAccessTest.test();
    }
}

/*
    2.2.3 以文本格式储存对象
 */

class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;
    public static final int NAME_SIZE = 30;
    public static final int RECORD_SIZE = 50;

    public Employee(String name, double salary, LocalDate hireDay) {
        this.name = name;
        this.salary = salary;
        this.hireDay = hireDay;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public LocalDate getHireDay() {
        return hireDay;
    }

    public void setHireDay(LocalDate hireDay) {
        this.hireDay = hireDay;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", hireDay=" + hireDay +
                '}';
    }
}

class TextFileTest {

    public static void test() {
        Employee[] staff = new Employee[]{
                new Employee("A", 10, LocalDate.now()),
                new Employee("B", 20, LocalDate.now()),
                new Employee("C", 30, LocalDate.now())
        };

        try(PrintWriter out = new PrintWriter("employee.dat","UTF-8")) {

            writeData(staff,out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try (Scanner in = new Scanner(new FileInputStream("employee.dat"))) {

            Employee[] newStaff = readData(in);

            for (Employee e : newStaff) {
                System.out.println(e);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    private static void writeData(Employee[] employees, PrintWriter out) {
        out.println(employees.length);
        for (Employee e : employees) {
            writeEmployee(out,e);
        }
    }

    private static void writeEmployee(PrintWriter out, Employee employee) {
        out.println(employee.getName()+"|"+employee.getSalary()+"|"+ employee.getHireDay());
    }

    private static Employee[] readData(Scanner in) {
        int n = in.nextInt();
        in.nextLine();

        Employee[] employees = new Employee[n];
        for (int i = 0; i < n; i++) {
            employees[i] = readEmployee(in);
        }
        return employees;
    }

    private static Employee readEmployee(Scanner in) {
        String line = in.nextLine();
        String[] tokens = line.split("\\|");

        String name = tokens[0];
        double salary = Double.parseDouble(tokens[1]);

        //*****************有趣的写法****************
        LocalDate hire = LocalDate.parse(tokens[2]);
        //******************************************

        return new Employee(name, salary, hire);
    }
}

《Java核心技术卷二》笔记

最新文章

  1. 关于C语言宏定义 使用do{ xxxx }while()
  2. android 数据存储Ⅱ
  3. HDU 1548 A strange lift (最短路/Dijkstra)
  4. Raising Modulo Numbers
  5. Python ~~~ 面向对象的利器
  6. 李洪强iOS开发支付集成之银联支付
  7. &quot;Money, Money, Money&quot;
  8. java 连接sql server2008配置
  9. [置顶] 与小伙伴共勉的java有关jvm的知识(一),小鸟尽量写得详细哦,欢迎讨论,谢绝喷子
  10. 《JavaScript权威指南》拾遗(下)
  11. 火狐浏览器打开html文件,中文乱码
  12. PowerDesigner设置null约束
  13. python静态属性的理解
  14. Android 音视频深入 四 录视频MP4(附源码下载)
  15. linux设置系统时间和时区
  16. C内存开辟与平移
  17. [Android 动画]简要分析一下Animator 与 Animation
  18. 【HTML入门】Html中块状元素和内联元素解析
  19. SpringCloud 入门
  20. 安装webpack命令环境

热门文章

  1. git pull和fetch的区别
  2. C#通过HttpListener实现HTTP监听
  3. CROSS JOIN
  4. Android零基础入门第35节:Android中基于回调的事件处理
  5. LigerUI中Grid的使用时关于url请求不到数据的问题
  6. Windows Azure之Mobile Service
  7. IntelliJ IDEA的jsp中内置对象方法无法被解析的解决办法
  8. Voovan 是一个高性能异步网络框架和 HTTP(Java)
  9. 关于客户端javascript的理解及事件浅析
  10. python算法与数据结构-单链表(38)