String类,Date和IO的综合练习

String类:
String代表不可变的字符序列,简称:不可变性
体现:1.当对字符串重新赋值时,需要重新指定内存区域赋值,不能使用原有的value进行赋值。
当对现有的字符串进行连接操作时,也需要重新指定内存区域赋值,不能使用原有的value进行   赋值。
当调用String的replace()方法修改指定字符或字符串时,也需要重新指定内存区域赋值,不能使     用原有的value进行赋值。

Date类:
1.Date date = new Date();
此时的date对象代表的是当前时间。
date.getTime();返回一个long型的数量代表 从1970年1月1日到当前时间所经历的毫秒数。

2.Date date = new Date();
   此时的date对象代表的是当前时间。
   date.getTime();返回一个long型的数量代表 从1970年1月1日到当前时间所经历的毫秒数

题目:读取C盘的a.txt文档,把文档中的内容解析为数据1封装在学生信息中。

Student类

package com.hp.text3;
 
import java.util.Date;
 
public class Student {
    private String name;
    private String sex;
    private Date birthday;
    private String address;
 
    public Student() {
 
    }
 
    public Student(String name, String sex, Date birthday, String address) {
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
        this.address = address;
 
 
    }
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    public Date getBirthday() {
        return birthday;
    }
 
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
 
 
}

ZongHeTest类

package KeTangText.FileAndStringZH;
 
import KeTangText.FileAndStringZH.Student;
 
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
 
public class ZongHeTest {
    public static void main(String[] args) throws IOException, ParseException {
        //1.定义File对象,关联映射磁盘文件
        File f1=new File("c:/a.txt");
        //定义缓冲字符流
        FileReader fr=new FileReader(f1);
        BufferedReader br=new BufferedReader(fr);
        //对文档进行读取每次读取一行数据
        ArrayList<Student> list=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");
 
        String temp;
        while (true){
            temp= br.readLine();//读取一行数据
            if (temp!=null){
                System.out.println(temp);//张三-男-2000/08/15-河南南阳
                String[] splist=temp.split("-");//把字符串按照-解析出来4个数据存入数组中,[张三, 男, 2000/08/15, 河南南阳]
                System.out.println(Arrays.toString(splist));
                //每一行内容对应的数组元素取出来封装到Student对象中
                Student student=new Student();
                student.setName(splist[0]);
                student.setSex(splist[1]);
                student.setBrithday(sdf.parse(splist[2]));//去除数组中第三个元素,是一个日期字符串,把他转换为对应的日期
                student.setAddress(splist[3]);
 
                list.add(student);
            }else{
                break;
            }
        }
        System.out.println(list);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值