文本存储实例

本例来源于java核心技术卷二


一、效果

将数据写入文本中,采用统一格式进行存储例如:

在这里插入图片描述

二、编写代码

1.Employee类

读的readData方法:

 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();
    }

写的writeData方法:

  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));
    }

2.TextFileTest类

私有方法Employee[] readData:

    private static Employee[] readData(Scanner in){
        int n=in.nextInt();
        in.nextInt();
        Employee[] employee=new Employee[n];
        for (int i=0;i<n;i++){
            employee[i]=new Employee();
            employee[i].readData(in);
        }
        return employee;
    }

私有方法writeData:

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

2.完整代码

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class TextFileTest {
    public static void main(String[] args) {
        Employee[] staff=new Employee[2];
        staff[0]=new Employee("shuxin",1000,2018,1,25);
        staff[1]=new Employee("lin",23000,2017,13,50);

        try {
            PrintWriter out=new PrintWriter("E:\\OK.txt");
            writeData(staff,out);
            out.close();

            Scanner in=new Scanner(new FileReader("E:\\OK.txt"));
            Employee[] newstaff=readData(in);
            in.close();

            for (Employee e:newstaff)
                System.out.println(e);

        }catch (IOException exception){
            exception.printStackTrace();

        }
    }

    private static Employee[] readData(Scanner in){
        int n=in.nextInt();
        in.nextInt();
        Employee[] employee=new Employee[n];
        for (int i=0;i<n;i++){
            employee[i]=new Employee();
            employee[i].readData(in);
        }
        return employee;
    }

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

//    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]=new Employee();
//            employees[i].readData(in);
//        }
//        return employees;
//    }

}

class Employee{
    private String name;
    private double salary;
    private Date hireDay;

    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 toString(){
        return getClass().getName()+"[name="+name+",salary="+salary+",hireDay="+hireDay+"]";
    }

    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();
    }

    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));
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public Date getHireDay() {
        return hireDay;
    }
}

总结

写代码一定要写注释!写这个的时候我在中途停了一段时间,后来继续在写的时候,发现我忘了之前写的思路是什么了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值