package StudentIO;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

//存方法
public class StoreDeta {
    //存方法
    //一、键盘录入三个学生存入集合
    public static ArrayList<StudentModel> setArrlist(){
        //1、创建键盘录入对象,创建集合对象
        Scanner sc = new Scanner(System.in);
        ArrayList<StudentModel> ob = new ArrayList<StudentModel>();
        //2、从客户输入存数据进去学生对象
//        for (int x = 0;x<3;x++){
        StudentModel student = new StudentModel();
        System.out.print("请输入学生ID:");
        String id = sc.next();
        student.setId(id);


        System.out.print("请输入学生名字:");
        String s = sc.next();
        student.setName(s);

        System.out.print("请输入学生年龄:");
        String i = sc.next();
        student.setAge(i);

        System.out.print("请输入学生爱好:");
        String z = sc.next();
        student.setHobby(z);

        //3、存数据进去集合
        ob.add(student);
//        }
        //4、释放输入流资源
        sc.close();
        return ob;

    }
    //二、把集合中的数据存入文本
    public static void write(ArrayList<StudentModel> studentarr) throws IOException {
        //1、遍历集合存入文本
        BufferedWriter bw = new BufferedWriter(new FileWriter("nba.txt"));
        for (int i = 0;i<studentarr.size();i++){
            StudentModel s = studentarr.get(i);
            StringBuilder sb = new StringBuilder();
            sb.append(s.getId()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getHobby());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }
}
最近下载更多
29723247  LV8 2024年12月6日
krispeng  LV14 2024年7月11日
HUANGWANG438  LV3 2023年12月6日
poipoiiop  LV8 2023年1月3日
lsglsg9  LV23 2022年9月28日
45645646  LV5 2022年6月22日
2390286321  LV8 2022年6月20日
1719863922  LV11 2022年6月17日
杨小姐好棒棒  LV3 2022年5月23日
Aaakin  LV1 2022年4月30日
最近浏览更多
2636804923  LV6 2月24日
571818771  LV3 2024年12月17日
曾多次  LV1 2024年12月8日
29723247  LV8 2024年12月6日
李清清  LV2 2024年11月6日
wuyanmingsi  LV1 2024年9月7日
krispeng  LV14 2024年7月11日
TY0165  LV20 2024年6月19日
微信网友_6906962132258816  LV7 2024年3月16日
quartz  LV8 2024年3月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友