字节流结合类,实现简单录入信息,并在本地存储

/*
字节流结合类,实现简单录入信息,并在本地存储

 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) throws IOException {

        ArrayList<Student> list = new ArrayList<>();
//创建"学生信息"文件夹
        File file = new File("F:\\学生信息");
        file.mkdirs();

//键盘录入学生信息
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入学生姓名:");
            String name = sc.next();
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();
//将学生信息添加到集合
            list.add(new Student(name, age));
        }


//遍历集合得到每个学生的基础信息
        for (Student student : list) {
            String name = student.getName();
            String age = Integer.toString(student.getAge());
//创建以学生姓名命名的文本
            String s = name + "--" + age;
            File file1 = new File(file, name + ".txt");
            boolean newFile = file1.createNewFile();
            System.out.println(name + "信息创建" + newFile);
//写入学生信息到对应TXT文本
            FileOutputStream fos = new FileOutputStream(file1);
            fos.write(s.getBytes());

            fos.close();
        }

    }
}

基本学生类![在这里插入图片描述](https://img-blog.csdnimg.cn/20201214222604766.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0l2ZW53Zg==,size_16,color_FFFFFF,t_70#pic_center)

import java.util.Objects;

public class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

![```](https://img-blog.csdnimg.cn/20201214222720290.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0l2ZW53Zg==,size_16,color_FFFFFF,t_70#pic_center)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值