本人学生自己写的,写了一个礼拜
注意事项:
首先要在E盘下创建student文件夹,因为学生信息是保存在E盘student文件下的,就必须在E盘下创建一个名为student的文件夹。
如果没有E盘,也可以修改代码中的代码片段,所有设计到的E盘的路径改为你要保存的盘下的名为student文件。
获取方式一
代码获取,一共有三个类,File类,Student类,班级学生管理程序类。把三个类放在一个src下。
file
import java.io.*;
import java.util.LinkedList;
public class File类 {
public File类() {
}
//===============1===============
public void writer(String list) throws IOException {
String[] strings = list.split(",");
//在几班,从String中获取数字
String str = strings[4].trim();
String string1 = "";
if (str != null && !"".equals(str)) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
string1 += str.charAt(i);
}
}
}
int x = Integer.parseInt(string1);
//学号,从String中获取数字
String string2 = strings[0].trim();
String string3 = "";
if (string2 != null && !"".equals(string2)) {
for (int i = 0; i < string2.length(); i++) {
if (string2.charAt(i) >= 48 && string2.charAt(i) <= 57) {
string3 += string2.charAt(i);
}
}
}
int a = Integer.parseInt(string3);
//目录
File file = new File("E:\\student");
File file1 = new File("E:\\student\\classNum" + x);
String string = "";
String[] strings3 = file.list();
for (String s : strings3) {
string += s;
}
//获取目录下的所有文件名称
if (string.contains("E:\\student\\classNum" + x)) {
//文件.txt
String str1 = "E:\\student\\classNum" + x + "\\student" + a + ".txt";
BufferedWriter bw = new BufferedWriter(new FileWriter(str1, true));
bw.write(list);
bw.newLine();
bw.flush();
bw.close();
} else {
//不存在file1
String str2 = "E:\\student\\classNum" + x;
File file2 = new File(str2);
//创建新班级
file2.mkdir();
String str1 = str2 + "\\student" + a + ".txt";
BufferedWriter bw = new BufferedWriter(new FileWriter(str1, true));
bw.write(list);
bw.newLine();
bw.flush();
bw.close();
}
}
//===============2===============
public void delStudent(int stuNum) throws IOException {
String s1 = "stuNum=" + stuNum;
String s2 = getAll();
String[] strings1 = s2.split("\n");
//从全部信息中获得哪一个是学号是stuNum的学生信息
int x = 0;
for (int i = 0; i < strings1.length; i++) {
if (strings1[i].contains(s1)) {
x = i;
break;
}
}
//获得班号
String[] strings = strings1[x].split(",");
try {
String string2 = strings[4].trim();
String string3 = "";
if (string2 != null && !"".equals(string2)) {
for (int i = 0; i < string2.length(); i++) {
if (string2.charAt(i) >= 48 && string2.charAt(i) <= 57) {
string3 += string2.charAt(i);
}
}
}
//班号
int a = Integer.parseInt(string3);
String s3 = "E:\\student\\classNum" + a + "\\student" + stuNum + ".txt";
File file = new File(s3);
//判断是否删除成功
if (file.delete()) {
System.out.println("删除成功");
} else if (!file.exists()) {
System.out.println("删除失败,该学生不存在");
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
//获取全部学生信息,并返回学生信息的字符串;
public String getAll() throws IOException {
String s1 = "";
String s3 = "";
String s5 = "";
LinkedList<String> lii = new LinkedList<>();
File file = new File("E:\\student");
File[] files = file.listFiles();
for (File F : files) {
s1 += F.getAbsolutePath();
}
String[] strings = s1.split("E");
for (int i = 1; i < strings.length; i++) {
String s2 = "E" + strings[i];
File file1 = new File(s2);
File[] files1 = file1.listFiles();
for (File F : files1) {
s3 += F.getAbsolutePath();
}
}
String[] strings1 = s3.split("E");
for (int i = 1; i < strings1.length; i++) {
String s4 = "E" + strings1[i];
File file1 = new File(s4);
BufferedReader br = new BufferedReader(new FileReader(file1));
lii.add(br.readLine());
br.close();
}
for (int i = 0; i < lii.size(); i++) {
s5 += lii.get(i) + "\n";
}
return s5;
}
//===============3===============
public void age加() throws IOException {
String[] strings = getAll().split("\n");
for (int i = 0; i < strings.length; i++) {
String[] strings1 = strings[i].split(",");
//stuNum是几
String string2 = strings1[0].trim();
String string3 = "";
if (string2 != null && !"".equals(string2)) {
for (int j = 0; j < string2.length(); j++) {
if (string2.charAt(j) >= 48 && string2.charAt(j) <= 57) {
string3 += string2.charAt(j);
}
}
}
//学号
int a = Integer.parseInt(string3);
//classNum是几
String string4 = strings1[4].trim();
String string5 = "";
if (string4 != null && !"".equals(string4)) {
for (int k = 0; k < string4.length(); k++) {
if (string4.charAt(k) >= 48 && string4.charAt(k) <= 57) {
string5 += string4.charAt(k);
}
}
}
//班号
int b = Integer.parseInt(string5);
//age是几
String string6 = strings1[3].trim();
String string7 = "";
if (string6 != null && !"".equals(string6)) {
for (int l = 0; l < string6.length(); l++) {
if (string6.charAt(l) >= 48 && string6.charAt(l) <= 57) {
string7 += string6.charAt(l);
}
}
}
//年龄
int c = Integer.parseInt(string7) + 1;
String s = "E:\\student\\classNum" + b + "\\student" + a + ".txt";
File file1 = new File(s);
file1.delete();
File file = new File(s);
String s1 = strings1[0] + "," + strings1[1] + "," + strings1[2] + ", " + "age=" + c + "," + strings1[4];
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(s1);
bw.close();
} catch (FileNotFoundException e) {
}
}
System.out.println("操作成功");
}
//===============4===============
public void shift(int stuNum, int classNum, String classNum1) throws IOException {
//转移前的路径
String s1 = "E:\\student\\classNum" + classNum + "\\student" + stuNum + ".txt";
File file1 = new File(s1);
//转移后的路径
String s2 = "E:\\student\\classNum" + classNum1 + "\\student" + stuNum + ".txt";
File file = new File(s2);
try {
BufferedReader br = new BufferedReader(new FileReader(s1));
BufferedWriter bw = new BufferedWriter(new FileWriter(s2));
String line;
while ((line = br.readLine()) != null) {
String[] strings = line.split(",");
String s3 = "classNum=" + classNum1 + "}";
String s4 = strings[0] + "," + strings[1] + "," + strings[2] + "," + strings[3] + ", " + s3;
bw.write(s4);
bw.flush();
}
file.createNewFile();
br.close();
bw.close();
if (file.exists()) {
System.out.println("转入成功");
}
} catch (FileNotFoundException e) {
System.out.println("请输入正确转移前的班号或学号");
}
file1.delete();
}
//===============5===============
public void readStudent(int classNum) {
for (int i = 0; i < 100; i++) {
String path = "E:\\student\\classNum" + classNum + "\\student" + i + ".txt";
File file = new File(path);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
}
}
}
//===============6===============
public void read(File file) throws IOException {
File[] fileArr = file.listFiles();
if (fileArr != null) {
//遍历该file数组,得到每一个file对象
for (File f : fileArr) {
//判断该file对象是否是目录
if (f.isDirectory()) {
//是,递归调用
read(f);
} else {
//否,获取绝对路径字符串,并
BufferedReader br = new BufferedReader(new FileReader(f.getAbsolutePath()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
}
}
}
Student
public class Student {
private int stuNum;
private String name;
private String sex;
private int age;
private int classNum;
public Student() {
}
public Student(int stuNum, String name, String sex, int age, int classNum) {
this.stuNum = stuNum;
this.name = name;
this.sex = sex;
this.age = age;
this.classNum = classNum;
}
public int getStuNum() {
return stuNum;
}
public void setStuNum(int stuNum) {
this.stuNum = stuNum;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getClassNum() {
return classNum;
}
public void setClassNum(int classNum) {
this.classNum = classNum;
}
@Override
public String toString() {
return "Student{" + "stuNum=" + stuNum + ", name=" + name + ", sex=" + sex + ", age=" + age + ", classNum=" + classNum + '}';
}
}
班级学生管理程序
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
import static java.lang.Integer.parseInt;
public class 班级学生管理程序 {
public static void main(String[] args) throws IOException {
//创建File类对象
File类 file = new File类();
//存储学生对象
LinkedList<Student> list = new LinkedList<>();
outer:
//按键
while (true) {
String ch = input("+++++++++++++++++++++++++++++++++" +
"\n" + "=> 输入数字来选择操作:" +
"\n" + " 1-增加一个学生" +
"\n" + " 2-根据学号删除学生" +
"\n" + " 3-所有学生年龄加一" +
"\n" + " 4-转班" +
"\n" + " 5-输入班号列出该班所有学生" +
"\n" + " 6-显示所有学生信息" +
"\n" + " 7-退出" +
"\n" + "+++++++++++++++++++++++++++++++++");
int choice = parseInt(ch);
switch (choice) {
case 1:
list.add(inputStudent());
file.writer(list.getLast().toString());
break;
case 2:
String stuNUM = input("请输入要删除学生的学号:");
int stuNum = parseInt(stuNUM);
file.delStudent(stuNum);
break;
case 3:
file.age加();
break;
case 4:
Scanner sc = new Scanner(System.in);
System.out.println("输入转班学生的学号以及班号");
int 学号 = sc.nextInt();
int 班号 = sc.nextInt();
System.out.println("输入该学生要转移到其他班的班号");
String 班号1 = sc.next();
file.shift(学号, 班号, 班号1);
break;
case 5:
String classNUM = input("输入班号,来显示所以的学生:");
int classNum = parseInt(classNUM);
file.readStudent(classNum);
break;
case 6:
File file1 = new File("E:\\student");
file.read(file1);
break;
case 7:
break outer;
}
}
}
//输入
public static String input(String hint) {
Scanner sc = new Scanner(System.in);
System.out.println(hint);
String x = sc.next();
return x;
}
//输入学生信息,并返回学生对象;
public static Student inputStudent() {
int stuNum = Integer.parseInt(input("请输入学号:"));
String name = input("请输入姓名:");
String sex = input("请输入性别:");
int age = Integer.parseInt(input("请输入年龄:"));
int classnum = Integer.parseInt(input("请输入班号:"));
return new Student(stuNum, name, sex, age, classnum);
}
}
获取方式二
网盘获取java文件。
链接:https://pan.baidu.com/s/1ypAJySdUYLBYXRxiaDktBw
提取码:2233
运行效果
运行班级学生管理程序那个类,先输入数字再按回车键
然后在E盘的student文件下就会出现以下文件
如果获取想要word版的课程设计报告(可能写的不太好嘻嘻)
链接:https://pan.baidu.com/s/1RrQBPiUBzLzZdXuIwsHuHQ
提取码:2233