java IO学生管理系统(把数据存入文本)

package StudentManageIO;

public class Student {
int sno;
String sname;
int age;
String site;

@Override
public String toString() {
    return sno + "," + sname + "," + age + "," + site;
}
public String toStringConsole() {
    return sno + "\t" + sname + "\t" + age + "\t" + site+"\t";
}
public Student() {
}
public Student(int sno, String sname, int age, String site) {
    this.sno = sno;
    this.sname = sname;
    this.age = age;
    this.site = site;
}
public int getSno() {
    return sno;
}
public void setSno(int sno) {
    this.sno = sno;
}
public String getSname() {
    return sname;
}
public void setSname(String sname) {
    this.sname = sname;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public String getSite() {
    return site;
}
public void setSite(String site) {
    this.site = site;
}

}

文本存取数据:

package StudentManageIO;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class IoText {
    static ArrayList<Student> arr = null;
    static FileWriter fw = null;
    static InputStreamReader reader = null;
    static BufferedReader br = null;
    static Student st = null;

    public static void init() throws IOException {
        fw = new FileWriter("text.txt");
        fw.close();
    }

    public static ArrayList<Student> SelectText() throws IOException {// 查询学生
        arr = new ArrayList<>();
        File filename = new File("text.txt");// 
        reader = new InputStreamReader(new FileInputStream(filename));
        br = new BufferedReader(reader); // 
        while (true) {
            String line = br.readLine(); // 
            if (line != null) {
                st = new Student();
                String s[] = line.split(",");
                st.setSno(Integer.parseInt(s[0]));
                st.setSname(s[1]);
                st.setAge(Integer.parseInt(s[2]));
                st.setSite(s[3]);
                arr.add(st);
            } else {
                break;
            }
        }
        reader.close();
        br.close();
        return arr;
    }

    public static void AddText(String string) throws IOException {
        fw = new FileWriter("text.txt", true);
        fw.write(string.toString());
        fw.write("\r\n");
        fw.close();
    }

    public static void delText(int sno) throws IOException {//删除
        File filename = new File("text.txt");//
        reader = new InputStreamReader(new FileInputStream(filename));
        br = new BufferedReader(reader); // 
        ArrayList<String> lineString = new ArrayList<>();
        int count = 1;
        while (true) {
            String line = br.readLine(); //
            if (line != null) {
                String s[] = line.split(",");
                if (Integer.parseInt(s[0]) == sno) {
                    System.out.println("删除成功!");
                    count--;
                } else
                    lineString.add(line);
            } else {
                break;
            }
        }
        if (count == 1) {
            System.out.println("删除失败!");
        }
        fw = new FileWriter("text.txt");
        for (int i = 0; i < lineString.size(); i++) {
            fw.write(lineString.get(i));
            if (i != lineString.size() - 1)
                fw.write("\r\n");
        }
        fw.close();
        reader.close();
        br.close();
    }

    public static void UpdateText(Student st) throws IOException {// 修改
        arr = new ArrayList<>();
        File filename = new File("text.txt");// 读数据
        reader = new InputStreamReader(new FileInputStream(filename));
        br = new BufferedReader(reader); 
        int count = 1;
        while (true) {
            String line = br.readLine(); // 读取每一行
            if (line != null) {
                String s[] = line.split(",");
                if (Integer.parseInt(s[0]) == st.getSno()) {
                    st.setSno(Integer.parseInt(s[0]));
                    st.setSname(st.getSname());
                    st.setAge(st.getAge());
                    st.setSite(st.getSite());
                    System.out.println("修改成功");
                    count--;
                }
                arr.add(st);
            } else {
                break;
            }
        }
        if (count == 1) {
            System.out.println("修改失败!");
        }
        fw = new FileWriter("text.txt");
        for (int i = 0; i < arr.size(); i++) {
            fw.write(arr.get(i).toString());
            if (i != arr.size() - 1)
                fw.write("\r\n");
        }
        fw.close();
        reader.close();
        br.close();
    }
}

用户数据操作

package StudentManageIO;

import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    static Scanner sc = null;
    static ArrayList<Student> s = null;
    static Student t = null;

    public static void inputMessage() {
        t = new Student();
        System.out.print("请输入用户名 :");
        String sname = sc.next();
        System.out.print("请输入年龄:");
        int age = sc.nextInt();
        System.out.print("请输入居住地:");
        String site = sc.next();
        t.setSname(sname);
        t.setAge(age);
        t.setSite(site);
    }

    public static void selcetStudent(ArrayList<Student> s) {// 查询学生
        if (s.size() == 0)
            System.out.println("不好意思,目前没有学生信息可供查询,请回去重新选择你的操作");
        else {
            System.out.println("学号\t姓名\t年龄\t居住地\t");
            for (int i = 0; i < s.size(); i++) {
                System.out.println(s.get(i).toString());
            }
        }
    }

    public static ArrayList<Student> addStudent(ArrayList<Student> s) {// 添加
        sc = new Scanner(System.in);
        System.out.print("请输入学号 :");
        int sno = sc.nextInt();
        inputMessage();
        t.setSno(sno);
        s.add(t);
        return s;
    }

    public static void delStudent(ArrayList<Student> s) {// 删除学生
        sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生的学号:");
        int sno = sc.nextInt();
        for (int i = 0; i < s.size(); i++) {
            if (s.get(i).getSno() == sno) {
                s.remove(i);
                System.out.println("删除成功!");
                break;
            } else if (i == s.size() - 1)
                System.out.println("该学生不存在!");
        }
    }

    public static void updateStudent(ArrayList<Student> s) {// 修改学生
        sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生的学号:");
        int sno = sc.nextInt();
        for (int i = 0; i < s.size(); i++) {
            if (s.get(i).getSno() == sno) {
                inputMessage();
                t.setSno(sno);
                s.set(i, t);
                System.out.println("修改成功!");
                break;
            } else if (i == s.size() - 1)
                System.out.println("该学生不存在!");

        }
    }

    public static void select() {
        sc = new Scanner(System.in);
        int select = 0;
        while (select != 5) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.查看所有学生");
            System.out.println("2.添加学生");
            System.out.println("3.删除学生");
            System.out.println("4.修改学生");
            System.out.println("5.退出");
            select = sc.nextInt();
            switch (select) {
            case 1:
                selcetStudent(s);
                break;
            case 2:
                addStudent(s);
                break;
            case 3:
                delStudent(s);
                break;
            case 4:
                updateStudent(s);
                break;
            case 5:
                System.out.println("谢谢使用");
                break;
            default:
                System.out.println("输入错误,重新选择!");
                break;
            }
        }
    }

    public static void main(String[] args) {
        s = new ArrayList<>();
        select();
    }
}
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值