package StudentIO;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class AlterDate {
    //根据id修改数据
    //思路:1、定义输入id,把数据从TXT文件中取出来
    //      2、取出来的数据存入model之后按照一定的规则存入集合,之后遍历,根据id得出对应的数据,对应修改
    //      3、把修改后的数据重新存入txt
    public static void Alter() throws IOException {
        System.out.println("请输入id");
        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        BufferedReader BR = new BufferedReader(new FileReader("nba.txt"));
        String lines;
        ArrayList<StudentModel> ob = new ArrayList<StudentModel>();
        while ((lines = BR.readLine())!=null){
            String[] arr = lines.split(",");
            StudentModel model = new StudentModel();
            model.setId(arr[0]);
            model.setName(arr[1]);
            model.setAge(arr[2]);
            model.setHobby(arr[3]);
            //数据放入ob里面
            ob.add(model);
        }
        //遍历:
        BufferedWriter bw = new BufferedWriter(new FileWriter("nba.txt"));
        int i;
        for (i = 0;i<ob.size();i++){
            StudentModel mod = ob.get(i);
            if (id.equals(mod.getId())){
                System.out.println("请输入名字:");
                String name = sc.next();
                mod.setName(name);
                System.out.println("请输入age:");
                String age = sc.next();
                mod.setAge(age);
                System.out.println("请输入hobby:");
                String hobby = sc.next();
                mod.setHobby(hobby);
                StringBuilder sb = new StringBuilder();
                sb.append(mod.getId()).append(",").append(mod.getName()).append(",").append(mod.getAge()).append(",").append(mod.getHobby());
                bw.write(sb.toString());
                bw.newLine();
                bw.flush();
            }
        }
        sc.close();
        bw.close();



    }

}
最近下载更多
HUANGWANG438  LV3 2023年12月6日
poipoiiop  LV8 2023年1月3日
lsglsg9  LV22 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日
龙门客栈  LV9 2021年12月17日
2304360848  LV5 2021年6月29日
最近浏览更多
quartz  LV7 3月13日
暂无贡献等级
fff2003  LV6 1月2日
lshlsh 2023年12月25日
暂无贡献等级
颜菜菜  LV2 2023年12月21日
HUANGWANG438  LV3 2023年12月6日
jiemomo  LV12 2023年11月2日
851690469  LV1 2023年11月2日
wcheng  LV2 2023年10月18日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友